扣扣分享交流群:1125844267
一、背景
刚刚在测试别的项目组的项目时,突然发现人家的邮件内容怎么那么好看呢,再看看自己的,就几行字发过去了,不要说客户能不能满意,首先自己就感觉不好看,所以自己也搞一个试试。
二、展示
完成后的邮件内容:
三、代码实现
(1)前端模板(也是自己网上找的,根据自己实际情况做了修改):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="xxxx">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div style="background-color:#ECECEC; padding: 35px;">
<table cellpadding="0" align="center"
style="width: 800px;height: 100%; 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); ">{0}</font>
</th>
</tr>
<tr>
<td style="word-break:break-all">
<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">
尊敬的用户:</font>
</font>
</h2>
<!-- 中文 -->
<p>{1}</p><br>
<!-- 英文 -->
<h2 style="margin: 5px 0px; ">
<font color="#333333" style="line-height: 20px; ">
<font style="line-height: 22px; " size="4">
Dear user,</font>
</font>
</h2>
<p>{2}</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>xxxxx单位</p>
<p>办公电话:xxxxx</p>
<p>电子邮箱:xxxxx</p>
<p>通讯地址:xxxxx</p><br>
<p>此为系统邮件,请勿回复<br>
Please do not reply to this system email
</p>
<!--<p>©***</p>-->
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
(2)后端实现
读取邮件模板,替换其中的占位符:
title:邮件内容的标题,如我这儿的“邮箱验证/Email address verification”;
htmlPath:邮件模板地址,如果有多个模板的话可以由传参决定使用哪个,如注册邮箱验证模板地址:/templates/registerEmail.html;
contentCH:中文内容;
contentEN:英文内容;
其实中英文可以是一个参数,但是为了避免在java代码中拼接过多的html代码,所以选择两个参数来做
/**
* 读取邮件模板
* 替换模板中的信息
* @param title
* @param htmlPath
* @param contentCH
* @param contentEN
* @return
*/
public String buildContent(String title, String htmlPath, String contentCH,String contentEN) {
//加载邮件html模板
Resource resource = new ClassPathResource(htmlPath);
InputStream inputStream = null;
BufferedReader fileReader = null;
StringBuffer buffer = new StringBuffer();
String line = "";
try {
inputStream = resource.getInputStream();
fileReader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = fileReader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
logger.error("发送邮件读取模板失败",e);
} finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//替换html模板中的参数
String htmlText = MessageFormat.format(buffer.toString(), title, contentCH,contentEN);
return htmlText;
}
将以上方法返回的字符串当做发送内容发送邮件即可,以下以springboot发送邮件为例进行邮件的发送:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
@Resource
private JavaMailSender javaMailSender;
/**
* content:即为以上方法返回的html字符串
*/
public void sendSimpleMail(String to, String subject, String content){
MimeMessage message;
try {
message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(fromEmail);
helper.setTo(to);
helper.setSubject(subject);
StringBuffer sb = new StringBuffer();
sb.append(content);
helper.setText(sb.toString(), true);
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}