Search code examples
springtomcatthymeleaf

Thymeleaf : template might not exist or might not be accessible by any of the configured Template Resolvers


I have this code:

private static final String EMAIL_INLINEIMAGE_TEMPLATE_NAME = "templateemail.html";

@Bean
public TemplateEngine emailTemplateEngine() {
    templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(this.htmlTemplateResolver());
       )

    templateEngine.setTemplateEngineMessageSource(this.messageSource);
    return templateEngine;
}

private static ITemplateResolver htmlTemplateResolver() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setOrder(Integer.valueOf(0));
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateResolver.DEFAULT_TEMPLATE_MODE);
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    return templateResolver;
}


public void sendEmail(String emailAddress, String title, String body, Locale local, String image) {
    if (Boolean.parseBoolean(isEmailServiceActivated)) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
        try {
            mailMsg.setFrom(EMAIL_USERNAME);
            mailMsg.setTo(emailAddress);
            mailMsg.setSubject(title);

            // Prepare the evaluation context
            ctx.setLocale(local);
            ctx.setVariable("imageHeaderResourceName", HEADER_LOGO_IMAGE);
            ctx.setVariable("body", body);

            ctx.setVariable("imageResourceName", image);

            final String htmlContent = this.templateEngine.process(new ClassPathResource(EMAIL_INLINEIMAGE_TEMPLATE_NAME).getPath(), ctx);
            mailMsg.setText(htmlContent, true );

            mailMsg.addInline(HEADER_LOGO_IMAGE, new ClassPathResource(HEADER_LOGO_IMAGE ) , PNG_MIME);
            mailMsg.addInline(image, new ClassPathResource(image) , PNG_MIME);

        } catch (MessagingException e) {
            e.printStackTrace();
        }

        mailSender.send(mimeMessage);
    }
}

I have templateemail.html file under /templates/ directory. when I launch the sending email method I have this exception :

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "templateemail.html", template might not exist or might not be accessible by any of the configured Template Resolvers

I dont know if it's because the templateEngine can't find my file (I try even with tomcat absolute path and /bin directory but no way ) or I haven't configure the right Template Resolver. Thank you very much for your help. I


Solution

  • It work now by deleting ".html" in the name of template (the file has the html extension)

    private static final String EMAIL_INLINEIMAGE_TEMPLATE_NAME = "templateemail"