title: Using thymeleaf template for sending Mail with Spring
body: <p>I'm trying to use Thymeleaf template for Spring application to send mail, I am taking the reference from here 
  <a href="https://github.com/thymeleaf/thymeleafexamples-springmail/" rel="nofollow">https://github.com/thymeleaf/thymeleafexamples-springmail/</a></p>
  
  <p>I am not getting any error but still it doesn't work ..I am using the same code as given in github still no luck ...Can anybody suggest how to do that??</p>
  
  <p>Following is the method which is used to send mail ...</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>If I remove the line which is creating the html body using thymeleaf and send a ordinary mail it works but not with template ..</p>
  
  <p>This is the line which causing problem ... just redirect to error page</p>
  
  <pre><code>final String htmlContent = this.templateEngine.process("email-simple.html", ctx);
  </code></pre>
  
  <p>Following is the error I am getting ..</p>
  
  <pre><code>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)
  </code></pre>