我想使用百里香发送电子邮件以呈现电子邮件。这是我在Spring 4 applicationContext.xml中的配置:
<bean id="thymeleafMailEngine" class="org.thymeleaf.TemplateEngine">
<property name="templateResolver" ref="thymeleafMailResolver" />
</bean>
<bean id="thymeleafMailResolver" class="org.thymeleaf.templateresolver.FileTemplateResolver">
<property name="templateMode" value="XHTML" />
<property name="prefix" value="src/main/webapp/WEB-INF/templates/" />
<property name="characterEncoding" value="UTF-8" />
<property name="cacheable" value="false"/>
</bean>
<bean id="websiteMailSender" class="com.mycompany.service.MailSenderWithAttachmentSupport">
<property name="thymeleafEngine" ref="thymeleafMailEngine" />
<property name="thymeleafResolver" ref="thymeleafMailResolver" />
</bean>
要发送电子邮件,我使用:
MimeMessage msg=mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
//...
final Context ctx = new Context();
ctx.setVariables(model); //model is a Map<String,Object>
helper.setText(thymeleafEngine.process(template, ctx)); //line 89
} catch(MessagingException me) {
log.error("Cannot send email message");
}
它生成此异常:
org.thymeleaf.exceptions.TemplateInputException:解决错误
模板“ my_template.html”,模板可能不存在或可能不存在
可通过以下任何已配置的模板解析器访问
org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
在org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)处
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)在
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)在
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924)在
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898)在
com.mycompany.service.MailSenderWithAttachmentSupport.send(MailSenderWithAttachmentSupport.java:89)
最佳答案
我刚刚找到了解决方案:
<bean id="thymeleafMailResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="templateMode" value="XHTML" />
<property name="prefix" value="/WEB-INF/templates/" />
<property name="characterEncoding" value="UTF-8" />
<property name="cacheable" value="false"/>
</bean>