如何解决模板解析器无法以正确的方式呈现电子邮件上的 html 代码
问题是:模板解析器没有以正确的方式呈现电子邮件上的 html 代码。
澄清:我需要在电子邮件而不是 html 文件中将 html 代码作为字符串发送。
例如,我使用带有逻辑的 Template Engine 发送了 html 内容:
public void sendEmailFromTemplate(User user,String titleKey,String newEmail) {
String htmlCode = "<!DOCTYPE html>\n" +
"<html xmlns:th=\"http://www.thymeleaf.org\">\n" +
" <head>\n" +
" <Meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
" <link rel=\"shortcut icon\" th:href=\"@{|${baseUrl}/favicon.ico|}\" />\n" +
" </head>\n" +
" <body>\n" +
" <p th:text=\"#{email.activation.text1}\">\n" +
" [[#{email.activation.text1}]]\n" +
" </p>\n" +
" </body>\n" +
"</html>\n";
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setvariable(USER,user);
context.setvariable("mail",newEmail);
context.setvariable(BASE_URL,jHipsterProperties.getMail().getBaseUrl());
StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setorder(1);
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
htmlCode = templateEngine.process(htmlCode,context);
String subject = messageSource.getMessage(titleKey,null,locale);
sendEmail(user.getEmail(),subject,htmlCode,false,true);
}
它在电子邮件中呈现给我:
??email.activation.text1_en??
按照同样的逻辑,我使用了 SpringTemplateEngine
public void sendEmailFromTemplate(User user,String newEmail) {
String htmlCode = "<!DOCTYPE html>\n" +
"<html xmlns:th=\"http://www.thymeleaf.org\">\n" +
" <head>\n" +
" <Meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
" <link rel=\"shortcut icon\" th:href=\"@{|${baseUrl}/favicon.ico|}\" />\n" +
" </head>\n" +
" <body>\n" +
" <p th:text=\"#{email.activation.text1}\">\n" +
" [[#{email.activation.text1}]]\n" +
" </p>\n" +
" </body>\n" +
"</html>\n";
Locale locale = Locale.forLanguageTag(user.getLangKey());
Context context = new Context(locale);
context.setvariable(USER,user);
context.setvariable("mail",newEmail);
context.setvariable(BASE_URL,jHipsterProperties.getMail().getBaseUrl());
String content = templateEngine.process(htmlCode,context);
String subject = messageSource.getMessage(titleKey,locale);
sendEmail(user.getEmail(),content,true);
}
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}" />
</head>
<body>
<p th:text="#{email.activation.text1}">
[[#{email.activation.text1}]]
</p>
</body>
</html>
],template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1059)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1048)
at com.service.MailService.sendEmailFromTemplate(MailService.java:131)
at com.service.MailService.sendActivationEmail(MailService.java:166)
如果我删除行:
String content = templateEngine.process(htmlCode,context);
并发送 html 代码,如:
sendEmail(user.getEmail(),true);
然后我收到与第一种情况类似的结果:
[[#{email.activation.text1}]]
以正确的方式,消息必须是这样的:
Your account has been created,please click on the URL below to activate it:
...
常规呈现的消息。
谁能告诉我,我是不是遗漏了什么?如果有助于改进问题,我可以添加更多信息。
UPD #1: 尝试按照 Thymeleaf documentation 中的说明编写 stringTemplateResolver
:
StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setorder(Integer.valueOf(3));
// No resolvable pattern,will simply process as a String template everything not prevIoUsly matched
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
结果也是:
??email.activation.text1_en??
UPD #2:看起来这些 template-engines
无法在不实际使用 html
文件的情况下处理来自数据库的本地化密钥
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。