1、导入thymeleaf和jsp依赖

在main目录下创建webapp文件夹
项目右键选择Open Module Seetings

然后再web下添加webapp文件夹

2、配置视图解析器


3、将thymeleaf邮件模板放在templates下

4、发送邮件
@Autowired
JavaMailSender javaMailSender;
@Autowired
TemplateEngine templateEngine;
@PostMapping ("/send")
public String FindAccount(User user) throws MessagingException {
User account = userService.searchUserByEmail(user.getEmail());
if(account!=null){
String randomPassword = Utils.randomPassword();
userService.updateUserPwd(account.getId(),randomPassword);
MimeMessage msg =javaMailSender.createMimeMessage();
MimeMessageHelper helper =new MimeMessageHelper(msg,true);
helper.setSubject(“后台管理系统找回密码”);
Context context = new Context();
context.setVariable(“username”,account.getName());
context.setVariable(“password”,randomPassword);
String process = templateEngine.process(“mail.html”, context);
helper.setText(process,true);
helper.setFrom(“发送账号的邮箱”);
helper.setTo(user.getEmail());
helper.setSentDate(new Date());
javaMailSender.send(msg);
return “redirect:/login”;
}
return “login”;
}

邮件模板:

5、javaMail配置如下:
spring.mail.host=smtp.163.com
spring.mail.port=25
spring.mail.username=发邮件的账号
spring.mail.password=这里是授权码不是邮箱密码
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
Springboot整合Jsp并且使用thymeleaf做邮件模板
最新推荐文章于 2024-05-06 12:00:48 发布
本文介绍如何在Spring Boot项目中整合Thymeleaf与JavaMail,实现带有HTML模板的邮件发送功能。通过配置邮件服务器,自定义邮件模板,并利用JavaMailSender与TemplateEngine发送包含动态内容的邮件。

3512

被折叠的 条评论
为什么被折叠?



