首先引入jar包:
<!-- mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--这里使用thymeleaf作为模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
邮件发送需要的一些配置
#邮件
#设置编码格式
spring.mail.default-encoding=utf-8
spring.mail.host=smtp.163.com
spring.mail.protocol=smtps
#邮箱的授权码 自己去邮箱中获取
spring.mail.password=xxxx
[email protected]
spring.mail.properties.mail.smtp.auth= true
spring.mail.properties.mail.smtp.starttls.enable= true
spring.mail.properties.mail.smtp.starttls.required= true
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/email/
spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache: false
spring.thymeleaf.mode: LEGACYHTML5
配置一下静态资源拦截
@Slf4j
@EnableWebMvc
@Configuration
public class SpringInterceptorRegister implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/*
* // TODO Auto-generated method stub
* registry.addResourceHandler("/templates/**,/static/**")
* .addResourceLocations("classpath:/templates/")
* .addResourceLocations("classpath:/static/**");
* super.addResourceHandlers(registry);
*/
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
模板HTML中能用到
@Data
public class EmailParam {
private String content;// 内容
private String updatePerson;// 操作人员
private String updateDate;// 操作时间
private String remarks;// 备注
}
主要实现类:
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private TemplateEngine templateEngine;
public void thymeleafEmail(String from,String[] to, String subject,EmailParam emailParam) throws MessagingException {
MimeMessage mimeMessage =javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
// 利用 Thymeleaf 模板构建 html 文本
Context ctx = new Context();
// 给模板的参数的上下文
ctx.setVariable("emailParam", emailParam);
// 执行模板引擎,执行模板引擎需要传入模板名、上下文对象
// Thymeleaf的默认配置期望所有HTML文件都放在 **resources/templates ** 目录下,以.html扩展名结尾。
String emailText = templateEngine.process("template", ctx);
mimeMessageHelper.setText(emailText, true);
// FileSystemResource logoImage= new FileSystemResource("D:\\image\\logo.jpg");
//绝对路径
//FileSystemResource logoImage = new FileSystemResource(imgPath);
//相对路径,项目的resources路径下
ClassPathResource logoImage = new ClassPathResource("static/image/111.jpg");
// 添加附件,第一个参数表示添加到 Email 中附件的名称,第二个参数是图片资源
//一般图片调用这个方法
mimeMessageHelper.addInline("logoImage", logoImage);
//一般文件附件调用这个方法
ClassPathResource resource = new ClassPathResource("templates/order.xlsx");
mimeMessageHelper.addAttachment("order.xlsx", resource);
javaMailSender.send(mimeMessage);
}
调用:
@RequestMapping("/sendHtmlMail")
public String sendHtmlMail() {
try {
EmailParam emailParam = new EmailParam();
emailParam.setContent("测试一下邮件");
emailParam.setUpdatePerson("xxx");
emailParam.setRemarks("备注");
emailParam.setUpdateDate(new Date().toLocaleString());
//此处to数组输入多个值,即可实现批量发送
String [] to={"xxx.com"};
sendEmailUtils.thymeleafEmail(from, to, "这是一封测试邮件主题", emailParam);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "ok";
}
静态文件html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试邮件发送</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;">
<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 align="center" style="padding: 30px 0 20px 0, height: 30px">
<img src='cid:logoImage' alt='前途出国' title='前途出国' height="100px" width="180px">
</td>
</tr>
<tr>
<td bgcolor="#ffffff" style="padding: 0px 30px 20px 30px">
<h4>邮件测试:</h4>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="padding: 12px 0 3px">操作人员:<span
th:text="${emailParam.updatePerson}"></span></td>
</tr>
<tr>
<td style="padding: 12px 0 3px">操作时间:<span
th:text="${emailParam.updateDate}"></span></td>
</tr>
<tr>
<td style="padding: 12px 0 3px">具体内容:<span
th:text="${emailParam.content}"></span></td>
</tr>
<tr>
<td style="padding: 12px 0 3px">备注:<span
th:text="${emailParam.remarks}"></span></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
以上就完成了;
源码在更新完基本集成基础使用后更新出来!谢谢