2013-02-06 38 views
6

Tôi đang cố gắng sử dụng Thymeleaf mẫu cho ứng dụng Spring để gửi thư, tôi lấy tài liệu tham khảo từ đây https://github.com/thymeleaf/thymeleafexamples-springmail/Sử dụng thymeleaf mẫu để gửi Mail với mùa xuân

tôi không nhận được bất kỳ lỗi nhưng vẫn nó doesn' t làm việc .. Tôi đang sử dụng cùng một mã như được đưa ra trong github vẫn không may mắn ... Ai có thể đề nghị làm thế nào để làm điều đó ??

Sau đây là phương pháp được sử dụng để gửi mail ...

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("[email protected]"); 
    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); 
} 

Nếu tôi loại bỏ các dòng được tạo ra phần nội dung html sử dụng thymeleaf và gửi thư thông thường nó hoạt động nhưng không phải với mẫu ..

Đây là dòng mà gây ra vấn đề ... chỉ cần chuyển hướng tới trang bị lỗi

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

Sau đây là các lỗi tôi nhận được ..

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) 

Trả lời

8

tôi nhận nó làm việc cuối cùng ... tôi đã thay đổi dòng sau

final Context ctx = new Context(locale); 

với

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

Bây giờ tôi có thể gửi bất kỳ mẫu hoặc tập tin đính kèm bất cứ điều gì hoạt động tuyệt vời .. !!

+4

Tôi biết đây là câu trả lời cũ, nhưng một lý do không đi tuyến đường này là bạn sẽ phụ thuộc vào yêu cầu như @real gadha được đề cập. Điều này có thể là ok đối với một ứng dụng thử nghiệm, nhưng nếu sau này bạn sử dụng cấu trúc ApplicationEvent của Spring để lắng nghe các sự kiện và gửi email trong khi thực hiện logic khác, bạn có thể không có quyền truy cập vào yêu cầu. – bphilipnyc

3

Bạn gặp phải vấn đề này vì có vẻ như bạn không có thư mục thư bên trong thư mục WEB-INF/classes.

Theo cấu hình được đề cập bên dưới, nó đang chờ thư mục thư bên trong thư mục 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> 

Vì vậy, hãy tạo thư mục có tên "thư" trong thư mục "resouces" và giữ mẫu thư bên trong thư mục "thư". Đảm bảo rằng bạn có thư mục "mail" này bên trong thư mục WEB-INF/classes trong khi triển khai. Điều này sẽ giải quyết vấn đề của bạn

IMO, chúng tôi không nên sử dụng WebContext vì nó yêu cầu đối tượng "yêu cầu" và khớp nối mạnh thư với yêu cầu không phải là một ý tưởng hay.

0

Nếu bạn muốn định vị mẫu của mình ở đâu đó bên dưới/WEB-INF/nhưng không muốn sử dụng ServletContextTemplateResolver, hãy thử SpringResourceTemplateResolver, ví dụ:

TemplateResolver templateResolver = new SpringResourceTemplateResolver(); 
templateResolver.setPrefix("/WEB-INF/mailtemplates/"); 
Các vấn đề liên quan