2013-02-06 39 views
6

मैं मेल भेजने के लिए स्प्रिंग आवेदन के लिए Thymeleaf टेम्पलेट का उपयोग करने कोशिश कर रहा हूँ के साथ मेल भेजने के लिए thymeleaf टेम्पलेट का उपयोग करना, मैं यहाँ से https://github.com/thymeleaf/thymeleafexamples-springmail/स्प्रिंग

संदर्भ ले रहा हूँ मैं किसी भी त्रुटि नहीं मिल रहा है, लेकिन अभी भी यह नहीं करता है काम नहीं GitHub में दिए गए फिर भी प्रयास विफल ..मैं एक ही कोड का उपयोग कर रहा हूँ ... किसी को भी ऐसा करने के लिए कैसे बता सकते हैं ??

के बाद विधि है जो मेल भेजने के लिए प्रयोग किया जाता है अगर मैं लाइन जो thymeleaf का उपयोग कर html शरीर पैदा कर रही है को हटाने और एक साधारण मेल यह काम करता है भेजने लेकिन टेम्पलेट के साथ नहीं है ...

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); 
} 

..

इस लाइन जो समस्या खड़ी कर रहा ... बस त्रुटि के लिए पेज

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

के बाद त्रुटि मैं हो रही है ..

01 अनुप्रेषित है
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) 

उत्तर

8

मैं अंत में काम कर यह मिल गया ... मैं

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

साथ निम्न पंक्ति

final Context ctx = new Context(locale); 

बदल अब मैं किसी भी टेम्पलेट या लगाव कुछ भी काम करता है महान नहीं भेज पा रहा हूं .. !!

+4

मैं जानता हूँ कि यह एक पुरानी जवाब है, लेकिन एक कारण इस मार्ग से जाने के लिए नहीं है कि आप अनुरोध पर निर्भर @real गधा उल्लेख किया हो जाएगा है। यह एक परीक्षण आवेदन के लिए ठीक हो सकता है, लेकिन यदि आप बाद में वसंत के ApplicationEvent संरचना का उपयोग, जबकि अन्य तर्क से बाहर ले जाने की घटनाओं और आग ईमेल के लिए सुनने के लिए, आपको अनुरोध करने के लिए उपयोग नहीं हो सकता है। – bphilipnyc

3

इस समस्या हो रही है, क्योंकि ऐसा लगता है, तो आप वेब-INF/कक्षाएं फ़ोल्डर के अंदर एक मेल फ़ोल्डर नहीं है।

विन्यास नीचे उल्लेख के अनुसार, यह वेब-INF/कक्षाएं फ़ोल्डर के अंदर एक मेल फ़ोल्डर उम्मीद कर रही है।

<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> 

इसलिए, आपका "resouces" फ़ोल्डर के अंदर नाम "मेल" के साथ एक फ़ोल्डर बनाने और "मेल" फ़ोल्डर के अंदर मेल टेम्पलेट रहते हैं। सुनिश्चित करें कि आप वेब-INF/कक्षाएं फ़ोल्डर के अंदर इस "मेल" फ़ोल्डर मिले, जबकि तैनाती करें। यह आपकी समस्या का समाधान करना चाहिए

IMO, हम WebContext उपयोग नहीं करना चाहिए, क्योंकि यह एक "अनुरोध" वस्तु की आवश्यकता है और मजबूत युग्मन अनुरोध के साथ मेल में एक अच्छा विचार नहीं है।

0

यदि आप कहीं नीचे/वेब-आईएनएफ/अपने टेम्पलेट्स को ढूंढना चाहते हैं/लेकिन ServletContextTemplateResolver का उपयोग नहीं करना चाहते हैं, तो SpringResourceTemplateResolver, उदाहरण के लिए प्रयास करें।

TemplateResolver templateResolver = new SpringResourceTemplateResolver(); 
templateResolver.setPrefix("/WEB-INF/mailtemplates/");