spring-boot-starter-mail发送邮件

Spring Boot环境中发送freemarker模板的html邮件

1.pom.xml引入spring-boot-starter-mail

2.application.yml配置

3.发送邮件

4.创建freemarker模板

5.创建EmailHtmlParameEntity

6.创建EmailService

pom.xml引入spring-boot-starter-mail

<!--邮件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<!--freemarker模板-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

application.yml配置

spring:
	# 邮件
  mail:
    default-encoding: utf-8
    host: smtp.163.com
    password: #########
    port: 465
    username: #######@163.com
    properties:
      mail.smtp.auth: true
      mail.smtp.timeout: 25000
      mail.smtp.port: 465
      mail.smtp.socketFactory.port: 465
      mail.smtp.socketFactory.fallback: false
      mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory

发送邮件

创建freemarker模板

创建task-body.ftl模板放至resources下的templates中
在这里插入图片描述

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css">
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            color: #6a6a6a;
        }

        a {
            font-size: 13px
        }
    </style>
</head>
<body>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td height="10">&nbsp;</td>
    </tr>
    <tr>
        <td valign="top" style="border-left:1px solid #CCC; border-right:1px solid #CCC;border-top:1px solid #CCC;">
            <table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr>
                    <td height="92" style="background-color:rgba(51,204,255,0.6);">
<#--                        <a href='http://${webip}:${webport}${webpath}' title="自动化测试平台" target="_blank"><img-->
<#--                                    src="http://${webip}:${webport}/img/maillogo.png" alt="LuckyFrame" width="300"-->
<#--                                    height="92" border="0"/></a></td>-->
                </tr>
                <tr>
                    <td height="1" colspan="2">
                        <hr style="border-bottom:5px solid #f1f1f1; display:block;"/>
                    </td>
                </tr>
                <tr>
                    <td height="20" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                    <td height="40" colspan="2" style="font-size:12px; text-indent:25px;">
                        <div style="margin: 0px auto; padding: 0px 10px; width: 680px;">
                            <div style="color: rgb(77, 77, 77); line-height: 1.5; font-size: 14px; margin-bottom: 25px;">
                                <strong style="margin-bottom: 15px; display: block;">亲爱的Tester: 您好!以下是自动化任务【${jobname}
                                    】执行情况。</strong>
                                <p><b>自动构建状态: </b><span style="color: #5CACEE; ">${buildstatus}</span></p>
                                <p><b>自动重启TOMCAT状态: </b><span style="color: #5CACEE; ">${restartstatus}</span></p>
                                <br>
                                <p><b>本次任务预期执行用例共【<span style="color: #2828FF; ">${casecount}</span>】条,耗时【<font color='#2828FF'>${time}</font></b></p>
                                <p><b>用例执行成功: </b><span style="color: #28FF28; ">${casesuc}</span></p>
                                <p><b>用例执行失败: </b><span style="color: #FF0000; ">${casefail}</span></p>
                                <p><b>用例有可能由于脚本原因未成功解析被锁定:</b><span style="color: #AE57A4; ">${caselock}</span></p>
                                <p><b>用例由于长时间未收到接口Response未执行完成:</b><span style="color: #FFAD86; ">${caseunex}</span></p>
                                <p>&nbsp;</p>
                                <p> 此为自动化平台的系统邮件,请勿回复</p>
<#--                                <p> 请及时前往<a href='http://${webip}:${webport}${webpath}'>自动化平台</a>查看您的任务执行的更多细节</p>-->
                                <p>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td height="10" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                    <td height="40" colspan="2">&nbsp;</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
</body>
</html>

创建EmailHtmlParameEntity

public class EmailHtmlParameEntity {
    /** 邮件接收人 */
    private String[] to;
    /** 邮件主题 */
    private String subject;
    /** 邮件内容参数 */
    private Map<String, Object> model;

    public String[] getTo() {
        return to;
    }

    public void setTo(String[] to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public Map<String, Object> getModel() {
        return model;
    }

    public void setModel(Map<String, Object> model) {
        this.model = model;
    }
}

创建EmailService

public class EmailService {
    @Autowired
    private Configuration configuration;

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String form;

    public void sendHtml(EmailHtmlParameEntity entity){
        String[] to = entity.getTo();
        String subject = entity.getSubject();
        Map<String, Object> model = entity.getModel();

        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper( message,true,"UTF-8");
            helper.setFrom(form);
            helper.setTo(to);
            helper.setSubject(subject);

            try {
                configuration.setEncoding(Locale.getDefault(),"UTF-8");
                configuration.setDateFormat("yyyy-MM-dd HH:mm:ss");
                Template template = configuration.getTemplate("emailTemplate/task-body.ftl");
                String html = FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
                helper.setText(html,true);
            } catch (Exception e) {
                e.printStackTrace();
            }


        } catch (MessagingException e) {
            e.printStackTrace();
        }

        javaMailSender.send(message);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值