title: How to use SpringTemplateEngine when using Spring Boot
body: <p>I am using Thymeleaf SpringTemplateEngine to create a HTML e-mail on my Spring application. When I was using pure Spring MVC everything was perfect. Now with Spring Boot the class can't find my .html template. I think the problem is with ServletContext that is not returning the right path, but I can't figure out how to resolve it. So should I use another Context to process the template? Which one?</p>
  
  <p>This is my MailService working for pure Spring MVC.</p>
  
  <pre><code>@Service
  public class MailService {
  
      private JavaMailSenderImpl mailSender;
  
      private SpringTemplateEngine templateEngine;
  
      public MailService() {
          mailSender = new JavaMailSenderImpl();
          //mailSender config
  
          templateEngine = new SpringTemplateEngine();
          Set&lt;ITemplateResolver&gt; templatesResolvers = new HashSet&lt;ITemplateResolver&gt;();
  
          ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
          emailTemplateResolver.setPrefix("mail/");
          emailTemplateResolver.setTemplateMode("HTML5");
          emailTemplateResolver.setCharacterEncoding("UTF-8");
          emailTemplateResolver.setOrder(1);
          templatesResolvers.add(emailTemplateResolver);
  
          ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver();
          webTemplateResolver.setPrefix("/mail/");
          webTemplateResolver.setTemplateMode("HTML5");
          webTemplateResolver.setCharacterEncoding("UTF-8");
          webTemplateResolver.setOrder(2);
          templatesResolvers.add(webTemplateResolver);
  
          templateEngine.setTemplateResolvers(templatesResolvers);
      }
  
      public void sendReport(String nome, String email, String obra,
              long medicao, HttpServletRequest request,
              HttpServletResponse response, ServletContext context, Locale locale)
              throws MessagingException {
  
          String subject = "Novo relatório";
  
          final WebContext ctx = new WebContext(request, response, context,
                  locale);
          ctx.setVariable("nome", nome);
          ctx.setVariable("obra", obra);
          ctx.setVariable("data", DataUtils.getInstance().getDataString(medicao));
          ctx.setVariable("logo", "logo.jpg");
  
          final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
          final MimeMessageHelper message = new MimeMessageHelper(mimeMessage,
                  true, "UTF-8");
          message.setSubject(subject);
          try {
              message.setFrom(new InternetAddress(mailSender.getUsername(),
                      "Sistema"));
          } catch (UnsupportedEncodingException e) {
          }
          message.setTo(email);
  
          final String htmlContent = this.templateEngine.process(
                  "email-relatorio.html", ctx);
          message.setText(htmlContent, true);
  
          try {
              File file = new File(context.getRealPath("app/img/logo-pro.jpg"));
              InputStreamSource imageSource = new ByteArrayResource(
                      IOUtils.toByteArray(new FileInputStream(file)));
              message.addInline("logo.jpg", imageSource, "image/jpg");
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          this.mailSender.send(mimeMessage);
      }
  }
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>org.thymeleaf.exceptions.TemplateInputException: Error resolving template "email-relatorio.html", template might not exist or might not be accessible by any of the configured Template Resolvers
      at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924)
      at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898)
  </code></pre>