gpt4 book ai didi

java - 无法发送富文本电子邮件 - Thymeleaf + Spring 4

转载 作者:行者123 更新时间:2023-11-30 03:22:38 26 4
gpt4 key购买 nike

我正在尝试使用 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]

有人知道如何解决这个问题吗?

提前致谢

最佳答案

嘿,我在一些研究之前遇到了这个问题,经过一些研究,我找到了解决方法。将您的 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;
}

希望这有帮助!

关于java - 无法发送富文本电子邮件 - Thymeleaf + Spring 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30986717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com