最先咱们发邮件的时候是使用 JavaMail 来发送邮件,而在 Spring Boot 中, Spring Boot 帮咱们将 JavaMail 封装好了,是能够直接拿来使用的。html
代码清单:spring-boot-mail/pom.xmljava
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
代码清单:git
server: port: 8080 spring: application: name: spring-boot-mail mail: host: smtp.qq.com username: 136736247 password: xxxxxx default-encoding: UTF-8 fromAddr: 136736247@qq.com nickName: inwsy
这里我使用 QQ 邮箱做为邮件的发送方,其中的 password
并非咱们的 QQ 密码,这个密码须要咱们在 QQ 邮箱的设置里面本身申请的。以下图:github
其中的 spring.mail.fromAddr
和 spring.mail.nickName
这两个配置是我本身配置的,并非官方的配置,后续我会在代码中读这两个配置项。spring
代码清单:spring-boot-mail/src/main/java/com/springboot/springbootmail/service/impl/MailServiceImpl.javaspringboot
@Service public class MailServiceImpl implements MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.fromAddr}") private String from; @Value("${spring.mail.nickName}") private String nickName; @Override public void sendSimpleEmail(String to, String subject, String content) { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(nickName + "<" + from + ">"); simpleMailMessage.setTo(to); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(content); try{ javaMailSender.send(simpleMailMessage); logger.info("简易邮件发送成功"); } catch(Exception e) { logger.error("简易邮件发送异常", e); } } }
代码清单:spring-boot-mail/src/test/java/com/springboot/springbootmail/SpringBootMailApplicationTests.java网络
@Autowired MailService mailService; @Test public void sendSimpleEmail() { mailService.sendSimpleEmail("inwsy@hotmail.com", "测试邮件题目", "测试邮件内容"); }
这里邮件发送至本人的 Hotmail 邮箱。app
代码清单:spring-boot-mail/src/main/java/com/springboot/springbootmail/service/impl/MailServiceImpl.java异步
@Override public void sendHTMLEmail(String to, String subject, String content) { MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(new InternetAddress(from, nickName, "UTF-8")); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(content, true); javaMailSender.send(message); logger.info("HTML 模版邮件发送成功"); } catch (MessagingException e) { logger.error("HTML 模版邮件发送失败", e); } catch (UnsupportedEncodingException e) { logger.error("收件地址编码异常", e); } }
代码清单:spring-boot-mail/src/main/resources/templates/email.htmlide
<!DOCTYPE html> <html lang="zh" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>邮件模版</title> </head> <body> 这是邮件模版生成的邮件,能够点击连接查看详情。 <a href="#" th:href="@{ http://www.geekdigging.com/ }">查看详情。</a> 当前的Code为:<span th:text="${code}"></span> </body> </html>
这里添加了动态内容 code ,在平常的开发中,咱们使用发送验证码,动态生成内容是颇有必要的。
代码清单:spring-boot-mail/src/test/java/com/springboot/springbootmail/SpringBootMailApplicationTests.java
@Test public void sendHTMLTemplateMail() { Context context = new Context(); context.setVariable("code", "123456"); String emailHTMLContent = templateEngine.process("email", context); mailService.sendHTMLEmail("inwsy@hotmail.com", "测试 HTML 模版邮件", emailHTMLContent); }
代码清单:spring-boot-mail/src/main/java/com/springboot/springbootmail/service/impl/MailServiceImpl.java
@Override public void sendAttachmentsMail(String to, String subject, String content, String fileName, String filePath) { MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(new InternetAddress(from, nickName, "UTF-8")); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); messageHelper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("带附件邮件发送成功"); } catch (MessagingException e) { logger.error("带附件邮件发送失败", e); } catch (UnsupportedEncodingException e) { logger.error("收件地址编码异常", e); } }
注意: 若是须要发送多个附件,写多个 messageHelper.addAttachment(fileName, file);
便可。
代码清单:spring-boot-mail/src/test/java/com/springboot/springbootmail/SpringBootMailApplicationTests.java
@Test public void sendAttachmentsMail() { String fileName = "图片.jpg"; String filePath = "C:\\Users\\inwsy\\Downloads\\0370279582fe3e2a8012060c896a5dd.jpg"; mailService.sendAttachmentsMail("inwsy@hotmail.com", "测试带附件的邮件", "详细请查阅附件", fileName, filePath); }
在实际的开发过程当中,邮件发送失败是一件比较常常出现的事情,好比:网络堵塞、对方拒收等状况,通常在邮件系统的设计中,能够先将要发送的数据写入数据中,等发送完成后再修改标记位,再增长一个保障机制,例如增长一个定时任务,将一段时间内,发送失败并重试次数小于必定阀值的内容再次进行发送,若是邮件系统的压力过大,能够选择使用异步的方式来进行发送,好比使用消息队列进行承压。
http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html
原文出处:https://www.cnblogs.com/babycomeon/p/11669486.html