1. 获取授权码
2. 引入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
3. 配置文件
spring:
application:
name: ml-mail-server
mail:
password: 授权码-非邮箱密码
default-encoding: utf-8
host: smtp.163.com
username: 用户名
4. 代码示例:
/**
* @author ml
*/
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private JavaMailSender mailSender;
/**
* 邮件接收人
*/
private final String[] SET_TO = new String[]{"xx@163.com","xx@163.com"};
/**
* 邮件发送人
*/
private final String SET_FROM = "xx@163.com";
/**
* 发送纯文本
*/
@RequestMapping("/sendText")
public ResultResponse sendText(){
SimpleMailMessage message = new SimpleMailMessage();
//发送人
message.setFrom("xx@163.com");
//收件人(可批量)
message.setTo("xx@163.com");
// 邮箱标题
message.setSubject("Java发送邮箱Demo 纯文本");
// 邮箱内容
message.setText("测试 Spring Boot 集成邮箱发送功能的!");
// 抄送人
message.setCc("xxqq.com");
// 发送邮件
mailSender.send(message);
return ResultResponse.success();
}
/**
* 发送html
*/
@RequestMapping("/sendHtml")
public ResultResponse sendHtml() throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper mimeHelper = new MimeMessageHelper(message, true);
// 发件人
mimeHelper.setFrom("xx@163.com");
// 收件人
mimeHelper.setTo("xx@163.com");
// 邮箱标题
mimeHelper.setSubject("HTML");
// 邮箱内容
mimeHelper.setText("<h2>MimeMessageHelper测试</h2>", true);
// 仍然使用 JavaMailSender 接口发送邮件
mailSender.send(message);
return ResultResponse.success();
}
@RequestMapping("/addAttachment")
public ResultResponse sendMailWithAttachment() throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper mimeWithAttachment = new MimeMessageHelper(message, true);
// 发件人
mimeWithAttachment.setFrom("xx@163.com");
// 收件人
mimeWithAttachment.setTo(SET_TO);
// 邮箱标题
mimeWithAttachment.setSubject("发送邮件(带附件)");
// 邮箱内容
mimeWithAttachment.setText("添加附件..");
// 1 附件添加图片
mimeWithAttachment.addAttachment("1.png", new File("D:\\1.png"));
mimeWithAttachment.addAttachment("222.docx", new File("D:\\222.docx"));
mimeWithAttachment.addAttachment("333.xlsx", new File("D:\\222.docx"));
// 仍然使用 JavaMailSender 接口发送邮件
mailSender.send(message);
return ResultResponse.success();
}
@RequestMapping("/embeddedPic")
public ResultResponse mailWithEmbeddedPic() throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper embeddedPic = new MimeMessageHelper(message, true);
// 发件人
embeddedPic.setFrom(SET_FROM);
// 收件人
embeddedPic.setTo(SET_TO);
//抄送给自己
embeddedPic.setCc(SET_FROM);
// 邮箱标题
embeddedPic.setSubject("发送邮件");
// 邮箱内容
embeddedPic.setText("发送内嵌图片..<img src='cid:1.png'/>",true);
// 添加内嵌图片
embeddedPic.addInline("1.png", new FileSystemResource(new File("D:\\1.png")));
// 仍然使用 JavaMailSender 接口发送邮件
mailSender.send(message);
return ResultResponse.success();
}
}
5. 错误
5.1 邮件被识别为垃圾邮件
spring发邮件被识别为垃圾邮件:org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163
解决方案:
把发件人加入到抄送里面 embeddedPic.setCc(SET_FROM);