SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
spring-boot-mail
項(xiàng)目結(jié)構(gòu)
1.Maven工程依賴(lài)坐標(biāo)
注意:SpringBoot版本需為2.x
若spring boot版本為1.x,
2.SpringBoot配置文件
#開(kāi)發(fā)環(huán)境下,關(guān)閉Thymeleaf緩存;生產(chǎn)環(huán)境需開(kāi)啟增強(qiáng)并發(fā)能力 spring.thymeleaf.cache=false #郵件服務(wù)器地址,已QQ郵箱為例 spring.mail.host=smtp.qq.com #郵件服務(wù)器端口 spring.mail.port=587 #發(fā)件人郵箱地址 spring.mail.username=發(fā)件人郵箱地址 #發(fā)件人郵箱密碼 spring.mail.password=發(fā)件人郵箱密碼 # 構(gòu)建授權(quán)信息,用于進(jìn)行SMTP進(jìn)行身份驗(yàn)證 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true3.郵件工具類(lèi)
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);//設(shè)置發(fā)件人郵箱helper.setFrom(senderMailAddress);//設(shè)置收件人郵箱helper.setTo((String[]) valueMap.get("to"));//設(shè)置郵件標(biāo)題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);}}//發(fā)送郵件javaMailSender.send(mimeMessage);} catch (MessagingException e) {e.printStackTrace();}} }4.測(cè)試代碼
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", "測(cè)試郵件標(biāo)題");valueMap.put("content","測(cè)試郵件內(nèi)容");valueMap.put("filePathList", filePathList);mailTool.sendSimpleMail(valueMap);} }附:注冊(cè)成功郵件html代碼模板
原文鏈接
總結(jié)
以上是生活随笔為你收集整理的SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Bootstrap的坑--千万别踩
- 下一篇: Java Mail+Thymeleaf模