title: Thymeleaf Rich HTML email error
body: <p>first of all, I have read al questions about thymeleaf email rendering, I mean, the original tutorial:</p>
  
  <p><a href="http://www.thymeleaf.org/doc/articles/springmail.html" rel="nofollow noreferrer">http://www.thymeleaf.org/doc/articles/springmail.html</a></p>
  
  <p>And other solved stackoverflow questions about this, The thing is I cant reach to a proper solution, and it starts to be annoying... so, please, can you please help me with this?</p>
  
  <p>The problem, as many people, I want to send rich HTML emails, and here is my Thymeleaf configuration:</p>
  
  <pre><code>@Configuration
  public class ThymeleafConfig {
  
      @Bean
      public ClassLoaderTemplateResolver emailTemplateResolver(){
          ClassLoaderTemplateResolver emailResolver = new ClassLoaderTemplateResolver();
          emailResolver.setPrefix("/WEB-INF/mailTemplates/");
          emailResolver.setSuffix(".html");
          emailResolver.setTemplateMode("HTML5");
          emailResolver.setOrder(1);
          return emailResolver;
      }
  
      @Bean
      public ServletContextTemplateResolver webTemplateResolver() {
          ServletContextTemplateResolver webResolver = new ServletContextTemplateResolver();
          webResolver.setPrefix("/WEB-INF/templates/");
          webResolver.setSuffix(".html");
          webResolver.setTemplateMode("HTML5");
          webResolver.setOrder(2);
          return webResolver;
      }
  
      @Bean
      public SpringTemplateEngine templateEngine() {
          SpringTemplateEngine engine = new SpringTemplateEngine();
          engine.addTemplateResolver(emailTemplateResolver());
          engine.addTemplateResolver(webTemplateResolver());
          engine.addDialect(new SpringSecurityDialect());
          return engine;
      }
  
      @Bean
      public ThymeleafViewResolver thymeleafViewResolver() {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setTemplateEngine(templateEngine());
          resolver.setOrder(1);
          return resolver;
      }
  }
  </code></pre>
  
  <p>in the emailResolver.setPrefix("/WEB-INF/mailTemplates/"); line I tried to put the original example config instead, like
  emailResolver.setPrefix("/mail/");
  and reorganising files in the eclipse project. Now its going like this</p>
  
  <p><a href="https://i.stack.imgur.com/lEi6c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lEi6c.png" alt="Eclipse Project organization"></a></p>
  
  <p>The method who sends the emails goes like this:</p>
  
  <pre><code>public void sendRegistrationEmail(String contextPath, Locale locale, User user) throws MessagingException{
          String token = UUID.randomUUID().toString();
          userService.createVerificationToken(user, token);
  
          String recipientAddress = user.getEmail();
          String subject = "Register";
          String confirmationUrl = contextPath + "/regitrationConfirm.html?token=" + token;
  
          String imageResourceName= "/images/myLogo.jpg";
          final Context ctx = new Context(locale);
          ctx.setVariable("name", user.getShownUsername());
          ctx.setVariable("subscriptionDate", new Date());
          ctx.setVariable("logo", imageResourceName);
          ctx.setVariable("name", user.getShownUsername());
          ctx.setVariable("loginName", user.getUsername());
          ctx.setVariable("email", user.getEmail());
          ctx.setVariable("link", confirmationUrl);
  
          final String htmlContent = templateEngine.process("registrationTemplate", ctx);
  
          final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
          final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
          message.setFrom("&lt;" + env.getProperty("email.username") + "&gt;");
          message.setTo("&lt;" + recipientAddress + "&gt;");
          message.setSubject(subject);
          message.setText(htmlContent,true);
  
          mailSender.send(mimeMessage);
      }
  </code></pre>
  
  <p>And, of course, the error in which Tomcat says that the Servlet context cant render this template.</p>
  
  <p>I have debbugged inside the thymelead code, and when it has to render the template, I have checked how it tryes to render the mail with the ClassLoaderTemplateResolver:</p>
  
  <p><a href="https://i.stack.imgur.com/re8oR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/re8oR.png" alt="Debugging the template rendering"></a></p>
  
  <p>and its simply write in the log this message:</p>
  
  <pre><code>logger.trace("[THYMELEAF][{}] Template \"{}\" could not be resolved as resource \"{}\" with resource resolver \"{}\"", new Object[] {TemplateEngine.threadIndex(), templateName, resourceName, resourceResolver.getName()});
  </code></pre>
  
  <p>with this params:</p>
  
  <pre><code>resourceName = /WEB-INF/mailTemplates/registrationTemplate.html
  templateName = registrationTemplate
  resourceResolver.getName() = org.thymeleaf.resourceresolver.ClassLoaderResourceResolver
  </code></pre>
  
  <p>I am pretty sure that the error is a little thing in some configuration or anything like that, buy I don't now where to search it...</p>
  
  <p>The exception runs when it dont render the template with the ClassLoader, and then its try to use the second renter template, the Servlet one, and it crashes with this well-known error:</p>
  
  <pre><code>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]] con causa raíz
  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]
  </code></pre>
  
  <p>Any ideas? </p>
  
  <p>I have read this approach:
  <a href="https://stackoverflow.com/questions/26609226/using-multiple-template-resolvers-with-spring-3-2-and-thymeleaf-2-1-3-for-emails">Using multiple template resolvers with Spring 3.2 and Thymeleaf 2.1.3 for emails</a></p>
  
  <p>But it will be nice to get it work with the configuration like the Thymeleaf example, avoiding to send the request, etc</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>To add more info, here is the email template:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
    &lt;head&gt;
      &lt;title th:remove="all"&gt;Registration Template&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p th:text="#{email.registration.greeting(${name})}"&gt;
        Hello, Username!
      &lt;/p&gt;
      &lt;p&gt;
        Registering on &lt;span th:text="${#dates.format(subscriptionDate)}"&gt;Date&lt;/span&gt;.
      &lt;/p&gt;
      &lt;p&gt;Your data:&lt;/p&gt;
      &lt;ul th:remove="all-but-first"&gt;
        &lt;li th:text="#{email.registration.loginname(${loginName})}"&gt;Login: username&lt;/li&gt;
        &lt;li th:text="#{email.registration.shownusername(${shownUserName})}"&gt;show name: UserName&lt;/li&gt;
        &lt;li th:text="#{email.registration.email(${email})}"&gt;email: user@mail.com&lt;/li&gt;
      &lt;/ul&gt;
      &lt;p&gt;
        Register link:
      &lt;/p&gt;
      &lt;p th:text="${link}"&gt;&lt;/p&gt;
      &lt;p&gt;
        Thanks!
      &lt;/p&gt;
      &lt;p&gt;
        &lt;img src="sample.png" th:src="'cid:' + ${logo}" /&gt;
      &lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>