日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)

發布時間:2024/10/8 HTML 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring-boot-mail

項目結構

1.Maven工程依賴坐標

注意:SpringBoot版本需為2.x
若spring boot版本為1.x,

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>thymeleaf-action</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><parent><artifactId>spring-boot-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.0.7.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-mail</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies> </project>

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=true

3.郵件工具類

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代碼模板

原文鏈接

<div style="background-color:#ECECEC; padding: 35px;"><table cellpadding="0" align="center"style="width: 600px; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微軟雅黑, 黑體; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;"><tbody><tr><th valign="middle"style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #42a3d3; background-color: #49bcff; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;"><font face="微軟雅黑" size="5" style="color: rgb(255, 255, 255); ">注冊成功! (阿里云)</font></th></tr><tr><td><div style="padding:25px 35px 40px; background-color:#fff;"><h2 style="margin: 5px 0px; "><font color="#333333" style="line-height: 20px; "><font style="line-height: 22px; " size="4">親愛的 123456</font></font></h2><p>首先感謝您加入本**站!下面是您的賬號信息<br>您的賬號:<b>123456</b><br>您的密碼:<b>123456</b><br>您的郵箱:<b>123@**.com</b><br>您注冊時的日期:<b>2019年06月17天23時33分20秒</b><br>您注冊時的IP:<b>221.230.56.12</b><br>您注冊的地址:<b>江蘇省鎮江市</b><br>當您在使用本網站時,遵守當地法律法規。<br>如果您有什么疑問可以聯系管理員,Email: **@**.com</p><p align="right">BER分接口網</p><p align="right">2019年06月17號 23時33分20秒</p><div style="width:700px;margin:0 auto;"><div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;"><p>此為系統郵件,請勿回復<br>請保管好您的郵箱,避免賬號被他人盜用</p><p>?***</p></div></div></div></td></tr></tbody></table> </div>

總結

以上是生活随笔為你收集整理的SpringBoot框架+Thymeleaf模板引擎实现发送HTML格式邮件(可带附件)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。