无法发送富文本电子邮件 - Thymeleaf + Spring 4
Unable to Send Rich Text Email - Thymeleaf + Spring 4
我正在尝试使用 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]
有没有人知道如何解决这个问题?
提前致谢
下面的代码会有所帮助
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);
嘿,我 运行 在研究之前和之后都对这个问题有所了解,我找到了解决方法。将您的 Java 配置更改为类似这样的内容,您还必须将 html 内容移动到 /WEB-INF/classes/mail/ 因为 ClassLoaderTemplateResolver 在 类 文件的目录。
@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;
}
希望这对您有所帮助!
我正在尝试使用 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]
有没有人知道如何解决这个问题?
提前致谢
下面的代码会有所帮助
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);
嘿,我 运行 在研究之前和之后都对这个问题有所了解,我找到了解决方法。将您的 Java 配置更改为类似这样的内容,您还必须将 html 内容移动到 /WEB-INF/classes/mail/ 因为 ClassLoaderTemplateResolver 在 类 文件的目录。
@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;
}
希望这对您有所帮助!