Spring boot Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@Service
public class MailContentBuilder {
private TemplateEngine templateEngine;
@Autowired
public MailContentBuilder(TemplateEngine templateEngine) {
this.templateEngine=templateEngine;
}
public String build(String templateName,String user,String email) throws IOException {
Context context=new Context();
context.setVariable("user", "Alpha");
context.setVariable("email", "alpha@gmail.com");
String test=templateEngine.process(templateName, context);
return test;
}
}
这是我的邮件发件人方法。
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
//mimeMessage.setContent(mailContentBuilder.build("changepassword","alpha","ema il@email.com"), "text/html");
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
helper.setTo(auth0UserService.getUser(userid).getEmail());
helper.setFrom(fromUsername);
helper.setSubject("Password Change Confirmation");
helper.setText(mailContentBuilder.build("changepassword","alpha","email@email.com"), true);
javaMailSender.send(mimeMessage);
这是我的模板,在src / resources / templates
中<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Change password</title>
</head>
<body >
helloooo th:text="${user}"
</body>
</html>
这是它发送的内容,它不遵循表达式语言,而是按原样写入页面。不使用变量。
helloooo th:text="${user}"
答案 0 :(得分:1)
:text必须是html标签的属性,所以类似
<p th:text="helloooo ${user}" />
从http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-texts 一眼看,应该有效