最先咱們發郵件的時候是使用 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