SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring-boot-mail
項目結構
1.Maven工程依賴坐標
注意:SpringBoot版本需為2.x
若spring boot版本為1.x,
2.SpringBoot配置文件
#開發環境下,關閉Thymeleaf緩存;生產環境需開啟增強并發能力 spring.thymeleaf.cache=false #郵件服務器地址,已QQ郵箱為例 spring.mail.host=smtp.qq.com #郵件服務器端口 spring.mail.port=587 #發件人郵箱地址 spring.mail.username=發件人郵箱地址 #發件人郵箱密碼 spring.mail.password=發件人郵箱密碼 # 構建授權信息,用于進行SMTP進行身份驗證 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true3.郵件工具類
package org.example.utils;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context;import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Map;@Component public class MailTool {@Autowiredprivate JavaMailSender javaMailSender;@Value("${spring.mail.username}")private String senderMailAddress;@Autowiredprivate TemplateEngine templateEngine;public void sendSimpleMail(Map<String, Object> valueMap) {MimeMessage mimeMessage = null;try {mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);//設置發件人郵箱helper.setFrom(senderMailAddress);//設置收件人郵箱helper.setTo((String[]) valueMap.get("to"));//設置郵件標題helper.setSubject(valueMap.get("title").toString());//添加正文(使用thymeleaf模板)Context context = new Context();context.setVariables(valueMap);String content = templateEngine.process("mail", context);helper.setText(content, true);//添加附件if (valueMap.get("filePathList") != null) {String[] filePathList = (String[]) valueMap.get("filePathList");for (String filePath : filePathList) {FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));String fileName = filePath.substring(filePath.lastIndexOf(File.separator));helper.addAttachment(fileName, fileSystemResource);}}//發送郵件javaMailSender.send(mimeMessage);} catch (MessagingException e) {e.printStackTrace();}} }4.測試代碼
package org.example.utils;import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.util.HashMap; import java.util.Map;import static org.junit.Assert.*;@SpringBootTest @RunWith(SpringRunner.class) public class MailToolTest {@Autowiredprivate MailTool mailTool;@Beforepublic void setUp() throws Exception {}@Afterpublic void tearDown() throws Exception {}@Testpublic void sendSimpleMail() {String[] filePathList = new String[]{"文件地址1","文件地址2","文件地址3",};Map<String, Object> valueMap = new HashMap<String, Object>();valueMap.put("to", new String[]{"收件人1", "收件人2", "收件人3"});valueMap.put("title", "測試郵件標題");valueMap.put("content","測試郵件內容");valueMap.put("filePathList", filePathList);mailTool.sendSimpleMail(valueMap);} }附:注冊成功郵件html代碼模板
原文鏈接
總結
以上是生活随笔為你收集整理的SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bootstrap的坑--千万别踩
- 下一篇: Java Mail+Thymeleaf模