如何用Spring发送电子邮件

时间:2022-12-07 10:00:51

简介

Spring基于javax.mail封装了一套用于发送电子邮件的API,位于org.springframework.mail包下,下面将介绍下发送普通文本邮件和基于thymeleaf模版的邮件。

普通邮件

导入javax.mail依赖

  1. <!-- ...... -->
  2. <!-- 省略Spring依赖 -->
  3. <dependency>
  4. <groupId>com.sun.mail</groupId>
  5. <artifactId>javax.mail</artifactId>
  6. <version>1.6.2</version>
  7. </dependency>

Spring配置类

  1. @PropertySource(value="classpath:mail.properties")
  2. @Configuration
  3. public class SpringConfig {
  4. @Autowired
  5. Environment environment;
  6.  
  7. /**
  8. * Spring邮件配置
  9. */
  10. @Bean
  11. public JavaMailSenderImpl javaMailSenderImpl(){
  12. JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
  13. javaMailSenderImpl.setHost(environment.getProperty("mail.host"));
  14. javaMailSenderImpl.setUsername(environment.getProperty("mail.username"));
  15. javaMailSenderImpl.setPassword(environment.getProperty("mail.password"));
  16. javaMailSenderImpl.setDefaultEncoding(environment.getProperty("mail.encoding"));
  17. return javaMailSenderImpl;
  18. }
  19. }

邮件配置:mail.properties

  1. #QQ邮箱发送邮件服务器
  2. mail.host=smtp.qq.com
  3. #QQ邮箱账户名
  4. mail.username=xxxxxx@qq.com
  5. #QQ邮箱授权码,开启IMAP/SMTP服务时会生成
  6. mail.password=lrorsapyffcwbegg
  7. #邮件发送编码
  8. mail.encoding=UTF-8

测试

  1. @RunWith(SpringRunner.class)
  2. @ContextConfiguration(classes=SpringConfig.class)
  3. public class MailTest {
  4. @Autowired
  5. private JavaMailSenderImpl javaMailSenderImpl;
  6.  
  7. @Test
  8. public void testSimpleMail() throws MessagingException{
  9. MimeMessage message = javaMailSenderImpl.createMimeMessage();
  10.  
  11. //发送带附件和内联元素的邮件需要将第二个参数设置为true
  12. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  13.  
  14. //发送方邮箱,和配置文件中的mail.username要一致
  15. helper.setFrom("xxxxxx@qq.com");
  16.  
  17. //接收方
  18. helper.setTo("xxxxxx@qq.com");
  19.  
  20. //主题
  21. helper.setSubject("邮件测试");
  22.  
  23. //邮件内容
  24. helper.setText("<html><body><img src='cid:myphoto'></body></html>", true);
  25.  
  26. //邮件内容内嵌的图片,addInline中的contentId要和邮件内容中的cid相对应
  27. FileSystemResource res = new FileSystemResource(new File("e:/yc/1.jpg"));
  28. helper.addInline("myphoto", res);
  29.  
  30. //邮件附件
  31. FileSystemResource file = new FileSystemResource(new File("e:/yc/myphoto.zip"));
  32. helper.addAttachment("myphoto.zip", file);
  33.  
  34. javaMailSenderImpl.send(message);
  35. }
  36. }

如何用Spring发送电子邮件

基于thymeleaf模版的邮件

导入thymeleaf-spring依赖

  1. <dependency>
  2. <groupId>org.thymeleaf</groupId>
  3. <artifactId>thymeleaf-spring4</artifactId>
  4. <version>3.0.11.RELEASE</version>
  5. </dependency>

Spring配置类中添加thymeleaf模板配置

  1. @Bean
  2. public TemplateEngine emailTemplateEngine() {
  3. SpringTemplateEngine templateEngine = new SpringTemplateEngine();
  4. ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
  5. templateResolver.setResolvablePatterns(Collections.singleton("*.html"));
  6. templateResolver.setPrefix("/templates/");
  7. templateResolver.setSuffix(".html");
  8. templateResolver.setTemplateMode(TemplateMode.HTML);
  9. templateResolver.setCharacterEncoding("UTF-8");
  10. templateResolver.setCacheable(false);
  11. templateEngine.addTemplateResolver(templateResolver);
  12. return templateEngine;
  13. }

模版文件:/src/main/resources/templates/msg.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <style type="text/css">span{color:orange}</style>
  6. </head>
  7. <body>
  8. <p>
  9. 您好, <span th:text="${name}">xxx</span>,感谢您使用XXX服务,您正在进行邮箱验证,本次请求的验证码为:
  10. </p>
  11. <p><span th:text="${validCode}">1111</span>(为了保障您帐号的安全性,请在1小时内完成验证。)</p>
  12. <p>
  13. XXX团队<br />
  14. 2019年06月06日
  15. </p>
  16. </body>
  17. </html>

测试

  1. @Autowired
  2. private TemplateEngine emailTemplateEngine;
  3.  
  4. @Test
  5. public void testTemplateMail() throws MessagingException{
  6. MimeMessage message = javaMailSenderImpl.createMimeMessage();
  7. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  8. helper.setFrom("xxxxxx@qq.com");
  9. helper.setTo("xxxxxx@qq.com");
  10. helper.setSubject("邮件测试");
  11.  
  12. //thymeleaf模版解析成String
  13. Context ctx = new Context();
  14. ctx.setVariable("name", "小姐姐");
  15. ctx.setVariable("validCode", "88888888");
  16. String htmlContent = emailTemplateEngine.process("msg.html", ctx);
  17. helper.setText(htmlContent, true);
  18.  
  19. javaMailSenderImpl.send(message);
  20. }

如何用Spring发送电子邮件

以上就是如何用Spring发送电子邮件的详细内容,更多关于Spring发送电子邮件的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/seve/p/11281992.html