6

我正在尝试使用 Spring 应用程序的 Thymeleaf 模板发送邮件,我从这里获取参考 https://github.com/thymeleaf/thymeleafexamples-springmail/

我没有收到任何错误,但它仍然不起作用..我使用与 github 中给出的相同代码仍然没有运气...有人可以建议如何做到这一点吗?

以下是用于发送邮件的方法...

public void sendSimpleMail(
        final String recipientName, final String recipientEmail, final Locale locale) 
        throws MessagingException {
    final Context ctx = new Context(locale);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("subscriptionDate", new Date());
    ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
    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.html", ctx);
    message.setText(htmlContent /* isHtml */);

    // Send email
    System.out.println("........");
   this.mailSender.send(mimeMessage);
}

如果我删除使用 thymeleaf 创建 html 正文的行并发送普通邮件,它可以工作,但不能使用模板 ..

这是导致问题的行......只需重定向到错误页面

final String htmlContent = this.templateEngine.process("email-simple.html", ctx);

以下是我得到的错误..

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]
    at org.thymeleaf.resourceresolver.ServletContextResourceResolver.getResourceAsStream(ServletContextResourceResolver.java:74)
    at org.thymeleaf.TemplateParser.parseDocument(TemplateParser.java:254)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:812)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:769)
    at com.ivb.coep.other.EmailService.sendSimpleMail(EmailService.java:72)
    at com.ivb.coep.controller.MainController.sendTextMail(MainController.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

3 回答 3

9

我终于让它工作了......我改变了以下行

 final Context ctx = new Context(locale);

final WebContext ctx = new WebContext(request,request.getContext(),locale);

现在我可以发送任何模板或附件任何东西都很好..!!

于 2013-02-07T10:01:13.007 回答
4

您遇到此问题是因为您似乎在 WEB-INF/classes 文件夹中没有邮件文件夹。

根据下面提到的配置,它期望在 WEB-INF/classes 文件夹内有一个邮件文件夹。

<bean id="emailTemplateResolver"     class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
    <property name="prefix" value="mail/" />
    <property name="templateMode" value="HTML5" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="order" value="1" />
    <!-- Template cache is true by default. Set to false if you want -->
    <!-- templates to be automatically updated when modified.        -->
    <property name="cacheable" value="true" />
</bean>

因此,在“资源”文件夹中创建一个名为“邮件”的文件夹,并将邮件模板保存在“邮件”文件夹中。确保在部署时在 WEB-INF/classes 文件夹中获得了这个“邮件”文件夹。这应该可以解决您的问题

IMO,我们不应该使用 WebContext,因为它需要一个“请求”对象,并且将邮件与请求强耦合并不是一个好主意。

于 2013-05-10T11:48:18.703 回答
0

如果你想在 /WEB-INF/ 下找到你的模板但不想使用 ServletContextTemplateResolver,那么试试 SpringResourceTemplateResolver,例如。

TemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/mailtemplates/");
于 2015-10-26T08:15:49.227 回答