thymeleaf模板配置邮件发送

thymeleaf模板配置邮件发送

1、导包

在项目POM中导入依赖包

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-mailartifactId>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-thymeleafartifactId>
dependency>

2、目录结构

thymeleaf模板配置邮件发送_第1张图片

3、配置thymeleaf

spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html

在未配置的情况下,默认路径为resources/templates下

4、准备html模板tdcAlarmEmail.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

thymeleaf模板配置邮件发送_第2张图片

你可能感兴趣的:(springboot,spring,boot)