[关闭]
@TedZhou 2020-08-28T10:38:10.000000Z 字数 2510 阅读 285

Spring boot mail with thymeleaf template

java spring mail thymeleaf


发送邮件是网站必备的功能,如注册验证,忘记密码或者是给用户发送通知信息。早期我们使用JavaMail相关api来写发送邮件。后来spring推出了JavaMailSender简化了邮件发送的过程,再之后springboot对此进行了封装。

复杂的邮件内容一般使用html,thymeleaf模板可以简化html的生成。

pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-mail</artifactId>
  8. </dependency>

application.propertis

  1. spring.mail.host=smtp.exmail.qq.com
  2. spring.mail.username=noreply@qyqq.com
  3. spring.mail.password=password123456
  4. spring.mail.default-encoding=UTF-8
  5. #spring.mail.properties.mail.smtp.starttls.enable=true
  6. #spring.mail.properties.mail.smtp.starttls.required=true
  7. #spring.mail.properties.mail.smtp.socketFactory.port=465
  8. #spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
  9. #spring.mail.properties.mail.smtp.ssl.trust=smtp.exmail.qq.com
  10. #spring.mail.properties.mail.smtp.connectiontimeout=30000
  11. #spring.mail.properties.mail.smtp.timeout=30000
  12. #spring.mail.properties.mail.smtp.writetimeout=20000

MailService.java

  1. import javax.mail.MessagingException;
  2. import javax.mail.internet.MimeMessage;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.mail.javamail.JavaMailSender;
  6. import org.springframework.mail.javamail.MimeMessageHelper;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class MailService {
  10. @Value("${spring.mail.username}")//from address must be same as authorization user
  11. String mailFrom;
  12. @Autowired
  13. JavaMailSender mailSender;
  14. public void sendHtml(String mailTo, String subject, String html) throws MessagingException{
  15. MimeMessage mime = mailSender.createMimeMessage();
  16. MimeMessageHelper mail = new MimeMessageHelper(mime);
  17. mail.setFrom(mailFrom);
  18. mail.setTo(mailTo.split(";"));//支持多个接收者
  19. mail.setSubject(subject);
  20. mail.setText(html, true);
  21. mailSender.send(mime);
  22. }
  23. }

NotifyService.java

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. import org.thymeleaf.TemplateEngine;
  4. import org.thymeleaf.context.Context;
  5. @Service
  6. public class NotifyService {
  7. @Autowired
  8. private MailService mailService;
  9. @Autowired
  10. private TemplateEngine templateEngine;
  11. public void sendNotify(String mailTo, String subject, Context context) {
  12. new Thread(() -> {//开启线程异步发送邮件
  13. try {
  14. String html = templateEngine.process("mail/notify", context);
  15. mailService.sendHtml(mailTo, subject, html);
  16. //TODO: 发送成功
  17. } catch (Exception e) {
  18. //TODO: 发送失败
  19. e.printStackTrace();
  20. }
  21. }).start();
  22. }
  23. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注