thymeleaf模板配置郵件發送
1、導包
在項目POM中導入依賴包
<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>
2、目錄結構
3、配置thymeleaf
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
在未配置的情況下,默認路徑爲resources/templates下
4、準備html模板tdcAlarmEmail.html(節選)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>xx項目-報警通知</title>
<style>
.button {
background-color: #4CAF50;
border-radius: 12px;
border: none;
color: white;
padding: 10px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 18px;
margin: 4px 2px;
cursor: pointer;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.button:hover {
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
}
</style>
</head>
<body style="margin: 0;padding: 0;">
<br/>
<br/>
<table align="center" border="1" cellpadding="0" cellspacing="0" width="600px">
<tr>
<td>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
<tr>
<td bgcolor="#ffffff" style="padding: 0px 30px 20px 30px">
<h3 style="text-align: center">xx項目-報警通知</h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">車牌:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${plat}"></span></td>
</tr>
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">司機:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${contact1}"></span></td>
</tr>
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">手機:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${contact1Phone}"></span></td>
</tr>
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">設備:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${deviceCode}"></span></td>
</tr>
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">速度:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${speed}"></span></td>
</tr>
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">時間:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${startTime}"></span></td>
</tr>
<tr>
<td style="padding: 5px 0 5px 0; text-align: center" width="20%">位置:</td>
<td style="padding: 5px 0 5px 0" width="80%"><span th:text="${alarmPlace}"></span></td>
</tr>
<tr>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="padding: 10px 0px 10px 0px;color: #c70000;">
請及時處理報警信息!<br/>
</td>
</tr>
<tr>
<td style="padding: 10px 0px 10px 0px">
如需幫助,請聯繫管理員:<br/>
彭xx:[email protected]<br/>
吳 x:[email protected]<br/>
</td>
</tr>
</table>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center" style="padding: 5px 0px 5px 0px">
<a class="button" href="https://xxx.com" >查看詳情</a>
</td>
</tr>
</table>
</tr>
</table>
</body>
</html>
5、Email推送關鍵代碼(節選)
public class AlarmMessageServiceImpl implements AlarmMessageService {
protected final Log logger = LogFactory.getLog(this.getClass());
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
/**
* xx項目-報警推送
* @param alarm
*/
public void SendAlarmMsg_TDC(BdsAlarmModel alarm){
try {
Map<String,Object> alarmInfo = new HashMap<>();
alarmInfo.put("plat",(String) map.get("plat"));
alarmInfo.put("contact1",(String) map.get("contact1"));
alarmInfo.put("contact1Phone",(String) map.get("contact1_phone"));
alarmInfo.put("deviceCode",alarm.getDeviceCode());
alarmInfo.put("speed",alarm.getSpeed());
alarmInfo.put("startTime",DateUtils.DateToString(alarm.getStartTime()));
alarmInfo.put("alarmPlace",alarm.getAlarmPlace());
sendSimpleMail((String) map.get("email"),"xx項目-【"
+(String)map.get("plat")
+"】",alarmInfo,"tdcAlarmEmail");
} catch (Exception e) {
logger.error("發送報錯:即時, "+JSON.toJSONString(alarm) , e);
}
}
/**
* 使用thymeleaf模板引擎
* @param variables
* @param templateName
*/
public void sendSimpleMail(String to,String title,Map<String,Object> variables,String templateName){
MimeMessage mimeMessage = null;
try {
mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 設置發件人郵箱
helper.setFrom("[email protected]");
// 設置收件人郵箱
helper.setTo(to);
// 設置郵件標題
helper.setSubject(title);
// 添加正文(使用thymeleaf模板)
Context context = new Context();
context.setVariables(variables);
String content = templateEngine.process(templateName, context);
helper.setText(content, true);
// 發送郵件
mailSender.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
附錄
在項目的application.properties中配置qq郵箱用於推送
#電子郵箱配置
spring.mail.host=smtp.qq.com
[email protected]
#password爲QQ郵箱申請的授權嗎
spring.mail.password=iscbakyhxxxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true