Spring Boot 揭祕與實戰(七) 實用技術篇 - Java Mail 發送郵件

文章目錄html

  1. 1. Spring Boot 集成 Java Mail
  2. 2. 單元測試
  3. 3. 源代碼

Spring 對 Java Mail 有很好的支持。所以,Spring Boot 也提供了自動配置的支持。java

Spring Boot 集成 Java Mail

Spring Boot 中使用 Java Mail 很是簡單方便。修改 pom 文件,添加依賴。git

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

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

  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 文本的郵件、 內嵌圖片的郵件、 包含附件的郵件。spring

  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-actionspringboot

(完)微信

 

微信公衆號
相關文章
相關標籤/搜索