文章目录

  1. 1. Spring Boot 集成 Java Mail
  2. 2. 单元测试
  3. 3. 源代码

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

源代码

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

(完)

 

微信公众号

转载于:https://www.cnblogs.com/cnblog-long/p/7245002.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐