Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件

时间:2022-07-03 06:17:55

Spring 对 Java Mail 有很好的支持。因此,Spring Boot 也提供了自动配置的支持。

Spring Boot 集成 Java Mail

Spring Boot 中使用 Java Mail 非常简单方便。修改 pom 文件,添加依赖。

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

在 src/main/resources/application.properties 中配置邮件信息

  1. #email
  2. spring.mail.host=smtp.163.com
  3. spring.mail.username=请输入用户名
  4. spring.mail.password=请输入密码
  5. spring.mail.port=25
  6. spring.mail.protocol=smtp
  7. spring.mail.default-encoding=UTF-8

单元测试

我们通过编写单元测试用例,测试 Java Mail 是否正常工作。值得注意的是,邮件包括几种类型:简单文本的邮件、 HTML 文本的邮件、 内嵌图片的邮件、 包含附件的邮件。

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(WebMain.class)
  3. public class SpringMailTest {
  4. @Autowired
  5. private JavaMailSenderImpl mailSender;
  6. /**
  7. * 发送包含简单文本的邮件
  8. */
  9. @Test
  10. public void sendTxtMail() {
  11. SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
  12. // 设置收件人,寄件人
  13. simpleMailMessage.setTo(new String[] {"lianggzone@163.com"});
  14. simpleMailMessage.setFrom("lianglevel@163.com");
  15. simpleMailMessage.setSubject("Spring Boot Mail 邮件测试【文本】");
  16. simpleMailMessage.setText("这里是一段简单文本。");
  17. // 发送邮件
  18. mailSender.send(simpleMailMessage);
  19. System.out.println("邮件已发送");
  20. }
  21. /**
  22. * 发送包含HTML文本的邮件
  23. * @throws Exception
  24. */
  25. @Test
  26. public void sendHtmlMail() throws Exception {
  27. MimeMessage mimeMessage = mailSender.createMimeMessage();
  28. MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
  29. mimeMessageHelper.setTo("lianggzone@163.com");
  30. mimeMessageHelper.setFrom("lianglevel@163.com");
  31. mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【HTML】");
  32. StringBuilder sb = new StringBuilder();
  33. sb.append("<html><head></head>");
  34. sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p></body>");
  35. sb.append("</html>");
  36. // 启用html
  37. mimeMessageHelper.setText(sb.toString(), true);
  38. // 发送邮件
  39. mailSender.send(mimeMessage);
  40. System.out.println("邮件已发送");
  41. }
  42. /**
  43. * 发送包含内嵌图片的邮件
  44. * @throws Exception
  45. */
  46. @Test
  47. public void sendAttachedImageMail() throws Exception {
  48. MimeMessage mimeMessage = mailSender.createMimeMessage();
  49. // multipart模式
  50. MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
  51. mimeMessageHelper.setTo("lianggzone@163.com");
  52. mimeMessageHelper.setFrom("lianglevel@163.com");
  53. mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【图片】");
  54. StringBuilder sb = new StringBuilder();
  55. sb.append("<html><head></head>");
  56. sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p>");
  57. // cid为固定写法,imageId指定一个标识
  58. sb.append("<img src=\"cid:imageId\"/></body>");
  59. sb.append("</html>");
  60. // 启用html
  61. mimeMessageHelper.setText(sb.toString(), true);
  62. // 设置imageId
  63. FileSystemResource img = new FileSystemResource(new File("D:/images/01.jpg"));
  64. mimeMessageHelper.addInline("imageId", img);
  65. // 发送邮件
  66. mailSender.send(mimeMessage);
  67. System.out.println("邮件已发送");
  68. }
  69. /**
  70. * 发送包含附件的邮件
  71. * @throws Exception
  72. */
  73. @Test
  74. public void sendAttendedFileMail() throws Exception {
  75. MimeMessage mimeMessage = mailSender.createMimeMessage();
  76. // multipart模式
  77. MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
  78. mimeMessageHelper.setTo("lianggzone@163.com");
  79. mimeMessageHelper.setFrom("lianglevel@163.com");
  80. mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【附件】");
  81. StringBuilder sb = new StringBuilder();
  82. sb.append("<html><head></head>");
  83. sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p></body>");
  84. sb.append("</html>");
  85. // 启用html
  86. mimeMessageHelper.setText(sb.toString(), true);
  87. // 设置附件
  88. FileSystemResource img = new FileSystemResource(new File("D:/images/01.jpg"));
  89. mimeMessageHelper.addAttachment("image.jpg", img);
  90. // 发送邮件
  91. mailSender.send(mimeMessage);
  92. System.out.println("邮件已发送");
  93. }
  94. }

源代码

相关示例完整代码: springboot-action

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件