一、配置文件
spring:
# 发件人邮箱
from: 家医风采<huhyemail@163.com>
# 配置 SMTP 服务器地址
host: smtp.163.com
# 发送者邮箱
username: huhyemail@163.com
# 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
password: HBZTCBYLVPSTKJAJ
# 端口号465
port: 465
# 默认的邮件编码为UTF-8
default-encoding: UTF-8
properties:
mail:
transport:
protocol: smtp
smtp:
auth: true
ssl:
enable: true
starttls:
enable: true
connectiontimeout: 50000
timeout: 30000
writetimeout: 50000
二、邮件模版 resources/**/*.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:remove="all">测试信息</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="margin: 10px auto;">
<div style="border: 0;border-radius: 4px;box-shadow: 0 4px 12px rgba(0,0,0,.15);padding-top:40px;max-width: 800px;margin:0 auto;width: 100%;padding-bottom: 30px;">
<div style="padding:5px 30px;font-size: 15px;font-weight: 500;color:#333;"><span th:text="${name}"></span>,您好:</div>
<div style="padding:10px 30px;font-size: 14px;font-weight: 300;color:#333;">您的验证码为:<span th:text="${verificationCode}"></span></div>
<div style="padding:30px 30px;font-size: 16px;font-weight: 500;color:#333;"><span style="float: right;">家医风采活动</span></td></div>
</div>
</div>
</body>
</html>
三、java代码
1、接口:
package cn.hh.email.handler;
import cn.hh.email.param.EmailParam;
/**
* @author huhy
*/
public interface EmailHandler {
boolean sendMail(EmailParam param);
}
2、配置类:
package cn.hh.email.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 邮件配置类
* @author huhy
*/
@Component
@ConfigurationProperties(prefix = "email")
public class EmailConfig {
/** 邮件主题 */
private String emailSubject;
/** 邮件模板 */
private String emailTemplate;
public String getEmailSubject() {
return emailSubject;
}
public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
public String getEmailTemplate() {
return emailTemplate;
}
public void setEmailTemplate(String emailTemplate) {
this.emailTemplate = emailTemplate;
}
}
3、实现类:
package cn.hh.email.handler.impl;
import cn.hh.email.config.EmailConfig;
import cn.hh.email.constant.EmailConstant;
import cn.hh.email.handler.EmailHandler;
import cn.hh.email.param.EmailParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.internet.MimeMessage;
import java.util.Date;
/**
* 邮件发送类
* @author huhy
*/
@Component
public class EmailHandlerImpl implements EmailHandler {
private static final Logger logger = LoggerFactory.getLogger(EmailHandlerImpl.class);
@Autowired
private TemplateEngine templateEngine;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private EmailConfig config;
@Value(value = "${spring.mail.from}")
private String fromMailbox;
@Override
public boolean sendMail(EmailParam param) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject(config.getEmailSubject());
helper.setFrom(fromMailbox);
helper.setTo(param.getTargetMailbox());
helper.setSentDate(new Date());
// 这里引入的是Template的Context
Context context = new Context();
// 设置模板中的变量
context.setVariable(EmailConstant.NAME, param.getName());
context.setVariable(EmailConstant.VERIFICATION_CODE, param.getVerificationCode());
// 第一个参数为模板的名称
String process = templateEngine.process(config.getEmailTemplate(), context);
// 第二个参数true表示这是一个html文本
helper.setText(process,true);
javaMailSender.send(mimeMessage);
return true;
}catch (Exception e){
logger.error(e.getMessage(),e);
return false;
}
}
}