我正在尝试使用Thymeleaf + Spring 4发送一个富文本邮件。我已经在Thymeleaf的网站上关注了这个例子,但由于某种原因它会抛出异常。
这是我的配置:
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setCharacterEncoding("UTF-8");
resolver.setTemplateMode("HTML5");
resolver.setOrder(2);
return resolver;
}
@Bean
public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("/WEB-INF/views/mail/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF8");
resolver.setOrder(1);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
final SpringTemplateEngine engine = new SpringTemplateEngine();
final Set<TemplateResolver> templateResolvers = new HashSet<TemplateResolver>();
templateResolvers.add(templateResolver());
templateResolvers.add(emailTemplateResolver());
engine.setTemplateResolvers(templateResolvers);
return engine;
}
从上面的配置中可以看出,我有两个解析器;一个用于页面,一个用于电子邮件模板。基于我在网上完成的研究,当我尝试使用我拥有的模板启动电子邮件时,它尝试使用templateResolver()而不是emailTemplateResolver(),尽管订单设置正确。
这是我的电子邮件服务:
Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
private static Logger logger = LoggerFactory.getLogger(EmailService.class);
/*
* Send HTML mail (simple)
*/
public void sendSimpleMail(final Locale locale)
throws MessagingException {
final String recipientName = "Name";
final String recipientEmail = "Email";
// Prepare the evaluation context
final Context ctx = new Context(locale);
ctx.setVariable("name", recipientName);
// Prepare message using a Spring helper
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setSubject("Example HTML email (simple)");
message.setFrom("thymeleaf@example.com");
message.setTo(recipientEmail);
// Create the HTML body using Thymeleaf
final String htmlContent = this.templateEngine.process("email-simple", ctx);
message.setText(htmlContent, true /* isHtml */);
// Send email
this.mailSender.send(mimeMessage);
}
以下是抛出的异常:
org.springframework.web.util.NestedServletException: 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]
有没有人有任何想法如何解决这个问题?
提前致谢
答案 0 :(得分:2)
嘿,我在一些研究之前和之后遇到过这个问题,我找到了解决方法。将您的Java配置更改为此类,您还必须将您的html内容移动到/ WEB-INF / classes / mail / ,因为ClassLoaderTemplateResolver在classes目录中查找您的文件。
@Bean
public TemplateResolver templateResolver() {
TemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setCharacterEncoding("UTF-8");
resolver.setTemplateMode("HTML5");
resolver.setOrder(2);
return resolver;
}
@Bean
public TemplateResolver emailTemplateResolver() {
TemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("mail/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setOrder(1);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
final SpringTemplateEngine engine = new SpringTemplateEngine();
final Set<TemplateResolver> templateResolvers = new HashSet<TemplateResolver>();
templateResolvers.add(emailTemplateResolver());
templateResolvers.add(templateResolver());
engine.setTemplateResolvers(templateResolvers);
return engine;
}
希望这有帮助!
答案 1 :(得分:0)
以下代码将有助于实现
WebContext wc=new WebContext(request,response,servletContext,request.getLocale());//servletContext Spring Context
MimeMessage message = this.mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
wc.setVariable("name", "abc");// If you want to pass params to Template
final String htmlContent = this.templateEngine.process("TemplateName", wc);