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做邮件模板
最新推荐文章于 2023-02-15 15:58:15 发布