1. 获取授权码

image.png
image.png
授权码只出现一次, 注意保存好

2. 引入jar包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

3. 配置文件

  1. spring:
  2. application:
  3. name: ml-mail-server
  4. mail:
  5. password: 授权码-非邮箱密码
  6. default-encoding: utf-8
  7. host: smtp.163.com
  8. username: 用户名

4. 代码示例:

  1. /**
  2. * @author ml
  3. */
  4. @RestController
  5. @RequestMapping("/email")
  6. public class EmailController {
  7. @Autowired
  8. private JavaMailSender mailSender;
  9. /**
  10. * 邮件接收人
  11. */
  12. private final String[] SET_TO = new String[]{"xx@163.com","xx@163.com"};
  13. /**
  14. * 邮件发送人
  15. */
  16. private final String SET_FROM = "xx@163.com";
  17. /**
  18. * 发送纯文本
  19. */
  20. @RequestMapping("/sendText")
  21. public ResultResponse sendText(){
  22. SimpleMailMessage message = new SimpleMailMessage();
  23. //发送人
  24. message.setFrom("xx@163.com");
  25. //收件人(可批量)
  26. message.setTo("xx@163.com");
  27. // 邮箱标题
  28. message.setSubject("Java发送邮箱Demo 纯文本");
  29. // 邮箱内容
  30. message.setText("测试 Spring Boot 集成邮箱发送功能的!");
  31. // 抄送人
  32. message.setCc("xxqq.com");
  33. // 发送邮件
  34. mailSender.send(message);
  35. return ResultResponse.success();
  36. }
  37. /**
  38. * 发送html
  39. */
  40. @RequestMapping("/sendHtml")
  41. public ResultResponse sendHtml() throws MessagingException {
  42. MimeMessage message = mailSender.createMimeMessage();
  43. MimeMessageHelper mimeHelper = new MimeMessageHelper(message, true);
  44. // 发件人
  45. mimeHelper.setFrom("xx@163.com");
  46. // 收件人
  47. mimeHelper.setTo("xx@163.com");
  48. // 邮箱标题
  49. mimeHelper.setSubject("HTML");
  50. // 邮箱内容
  51. mimeHelper.setText("<h2>MimeMessageHelper测试</h2>", true);
  52. // 仍然使用 JavaMailSender 接口发送邮件
  53. mailSender.send(message);
  54. return ResultResponse.success();
  55. }
  56. @RequestMapping("/addAttachment")
  57. public ResultResponse sendMailWithAttachment() throws MessagingException {
  58. MimeMessage message = mailSender.createMimeMessage();
  59. MimeMessageHelper mimeWithAttachment = new MimeMessageHelper(message, true);
  60. // 发件人
  61. mimeWithAttachment.setFrom("xx@163.com");
  62. // 收件人
  63. mimeWithAttachment.setTo(SET_TO);
  64. // 邮箱标题
  65. mimeWithAttachment.setSubject("发送邮件(带附件)");
  66. // 邮箱内容
  67. mimeWithAttachment.setText("添加附件..");
  68. // 1 附件添加图片
  69. mimeWithAttachment.addAttachment("1.png", new File("D:\\1.png"));
  70. mimeWithAttachment.addAttachment("222.docx", new File("D:\\222.docx"));
  71. mimeWithAttachment.addAttachment("333.xlsx", new File("D:\\222.docx"));
  72. // 仍然使用 JavaMailSender 接口发送邮件
  73. mailSender.send(message);
  74. return ResultResponse.success();
  75. }
  76. @RequestMapping("/embeddedPic")
  77. public ResultResponse mailWithEmbeddedPic() throws MessagingException {
  78. MimeMessage message = mailSender.createMimeMessage();
  79. MimeMessageHelper embeddedPic = new MimeMessageHelper(message, true);
  80. // 发件人
  81. embeddedPic.setFrom(SET_FROM);
  82. // 收件人
  83. embeddedPic.setTo(SET_TO);
  84. //抄送给自己
  85. embeddedPic.setCc(SET_FROM);
  86. // 邮箱标题
  87. embeddedPic.setSubject("发送邮件");
  88. // 邮箱内容
  89. embeddedPic.setText("发送内嵌图片..<img src='cid:1.png'/>",true);
  90. // 添加内嵌图片
  91. embeddedPic.addInline("1.png", new FileSystemResource(new File("D:\\1.png")));
  92. // 仍然使用 JavaMailSender 接口发送邮件
  93. mailSender.send(message);
  94. return ResultResponse.success();
  95. }
  96. }

5. 错误

5.1 邮件被识别为垃圾邮件

spring发邮件被识别为垃圾邮件:org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163

解决方案:
把发件人加入到抄送里面 embeddedPic.setCc(SET_FROM);