首先,我已经阅读了有关百万美元电子邮件呈现的问题,我的意思是,原始教程:
http://www.thymeleaf.org/doc/articles/springmail.html
其他解决了stackoverflow关于此的问题,问题是我无法达到一个合适的解决方案,而且开始令人讨厌......所以,请你帮我解决这个问题吗?
问题,就像许多人一样,我想发送丰富的HTML电子邮件,这是我的Thymeleaf配置:
2 -1 11 -1
1 -1 11 -1
11 -1 1 -1
11 -1 1 -1 1 -1
11 -1 2 -1 1 -1
11 -1 1 -1 2 -1
11 -1 3 -1 1 -1
11 -1 1 -1 3 -1....
发送电子邮件的方法如下:
@Configuration
public class ThymeleafConfig {
@Bean
public ClassLoaderTemplateResolver emailTemplateResolver(){
ClassLoaderTemplateResolver emailResolver = new ClassLoaderTemplateResolver();
emailResolver.setPrefix("/WEB-INF/mailTemplates/");
emailResolver.setSuffix(".html");
emailResolver.setTemplateMode("HTML5");
emailResolver.setOrder(1);
return emailResolver;
}
@Bean
public ServletContextTemplateResolver webTemplateResolver() {
ServletContextTemplateResolver webResolver = new ServletContextTemplateResolver();
webResolver.setPrefix("/WEB-INF/templates/");
webResolver.setSuffix(".html");
webResolver.setTemplateMode("HTML5");
webResolver.setOrder(2);
return webResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addTemplateResolver(emailTemplateResolver());
engine.addTemplateResolver(webTemplateResolver());
engine.addDialect(new SpringSecurityDialect());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setOrder(1);
return resolver;
}
}
当然,Tomcat说Servlet上下文无法呈现此模板的错误。
我已经在百里香的代码中进行了调查,当它必须渲染模板时,我已经检查了它如何尝试使用ClassLoaderTemplateResolver渲染邮件:
它只需在日志中写下这条消息:
public void sendRegistrationEmail(String contextPath, Locale locale, User user) throws MessagingException{
String token = UUID.randomUUID().toString();
userService.createVerificationToken(user, token);
String recipientAddress = user.getEmail();
String subject = "Register";
String confirmationUrl = contextPath + "/regitrationConfirm.html?token=" + token;
String imageResourceName= "/images/myLogo.jpg";
final Context ctx = new Context(locale);
ctx.setVariable("name", user.getShownUsername());
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("logo", imageResourceName);
ctx.setVariable("name", user.getShownUsername());
ctx.setVariable("loginName", user.getUsername());
ctx.setVariable("email", user.getEmail());
ctx.setVariable("link", confirmationUrl);
final String htmlContent = templateEngine.process("registrationTemplate", ctx);
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setFrom("<" + env.getProperty("email.username") + ">");
message.setTo("<" + recipientAddress + ">");
message.setSubject(subject);
message.setText(htmlContent,true);
mailSender.send(mimeMessage);
}
用这个参数:
logger.trace("[THYMELEAF][{}] Template \"{}\" could not be resolved as resource \"{}\" with resource resolver \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateName, resourceName, resourceResolver.getName()});
我很确定这个错误在某些配置中是一个小东西或类似的东西,买我现在不在哪里搜索它...
异常在它不使用ClassLoader呈现模板时运行,然后它尝试使用第二个renter模板,Servlet模板,并且它崩溃了这个众所周知的错误:
resourceName = /WEB-INF/mailTemplates/registrationTemplate.html
templateName = registrationTemplate
resourceResolver.getName() = org.thymeleaf.resourceresolver.ClassLoaderResourceResolver
有什么想法吗?
我读过这种方法: Using multiple template resolvers with Spring 3.2 and Thymeleaf 2.1.3 for emails
但让它与Thymeleaf示例配置一起使用会很好,避免发送请求等等
提前致谢!
修改
要添加更多信息,请参阅电子邮件模板:
Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Resource resolution by ServletContext with org.thymeleaf.resourceresolver.ServletContextResourceResolver can only be performed when context implements org.thymeleaf.context.IWebContext [current context: org.thymeleaf.context.Context]] con causa raíz
org.thymeleaf.exceptions.TemplateProcessingException: Resource resolution by ServletContext with org.thymeleaf.resourceresolver.ServletContextResourceResolver can only be performed when context implements org.thymeleaf.context.IWebContext [current context: org.thymeleaf.context.Context]
答案 0 :(得分:3)
我几乎可以肯定这个问题来自于此配置:
ClassLoaderTemplateResolver emailResolver = new ClassLoaderTemplateResolver();
emailResolver.setPrefix("/WEB-INF/mailTemplates/");
当你在战争中时,/ WEB-INF / mailTemplates不在类路径上。而是将您的mailTemplates文件夹放在src / resources中。然后它将被置于战争中的/ WEB-INF / classes /中,您应该能够使用此配置访问它们:
ClassLoaderTemplateResolver emailResolver = new ClassLoaderTemplateResolver();
emailResolver.setPrefix("/mailTemplates/");
编辑: 另一个解决方案是对电子邮件模板使用ServletContextTemplateResolver:
ServletContextTemplateResolver emailResolver = new ServletContextTemplateResolver();
emailResolver.setPrefix("/WEB-INF/mailTemplates/");