注册时,我存储在html中的文本被发送到邮件中,我希望将用户的值分配给这些值并发送文本
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:remove="all">Template for HTML email</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="${'Hello Dear,' + ${name}}">
Hello Dear John Doe`</p>`
<p th:text="${'Welcome.Please visit, http://aa.ru/activate-account/' + ${activationCode}}">
Welcome. Please visit http://aa.ru/activate-account/%s
</p>
<p>
Thanks
</p>
</body>
</html>
private TemplateEngine springTemplateEngine = new TemplateEngine();
Context context = new Context();
context.setVariable("name",user.getUsername());
context.setVariable("activationCode",user.getActivationCode());
String text = springTemplateEngine.process("email-template.html",context);
if (!org.springframework.util.StringUtils.isEmpty(user.getEmail()))
{
mailSenderService.send(user.getEmail(), "Activation code", text);
}
邮件上应附有消息Hello Dear,萨米尔 欢迎使用。请访问http://aa.ru/activate-account/blabla
templateResolver.setResolvablePatterns(Collections.singleton("html/*"));
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
答案 0 :(得分:0)
我认为问题出在这一行。
private TemplateEngine springTemplateEngine = new TemplateEngine();
您似乎正在创建没有任何配置的新模板引擎。通常,当我在项目中发送电子邮件时,我使用在MvcConfig类中配置的模板引擎。所以,首先,让我向您展示我的MvcConfig类。
配置
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setCacheable(false);
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
@Bean
public ServletContextTemplateResolver servletContextTemplateResolverResolver() {
final ServletContextTemplateResolver resolver =
new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCacheable(false);
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(new UrlTemplateResolver());
templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
@Description("Thymeleaf view resolver")
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
现在,无论何时发送和发送电子邮件,我都使用以下几行。如您所见,我正在获取在MvcConfig类上配置的模板引擎。也许,这可能是您出错的原因。
MvcConfig config = new MvcConfig();
TemplateEngine templateEngine = config.templateEngine();