Piotr Kończak Piotr Kończak - 5 months ago 416
Java Question

Spring boot sending emails using Thymeleaf as template - configuration does not work

I have working WebApp based on Spring Boot (newest 1.1.5.RELEASE) and Thymeleaf.

Now I would like to add functionality to send emails and use Thymelaf as templating engine.



In pom.xml I add:


<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>


Following this tutorial: http://www.thymeleaf.org/doc/articles/springmail.html


got complete (no XML) Java configuration like that:

@Configuration
public ThymeleafReplaceConfigurator {
@Bean
public JavaMailSender getJavaMailSenderImpl(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

Properties props = new Properties();
/* some properties here */

javaMailSender.setJavaMailProperties(props);

return javaMailSender;
}

@Bean
public ClassLoaderTemplateResolver emailTemplateResolver(){
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("/mails/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode("HTML5");
emailTemplateResolver.setCharacterEncoding("UTF-8");
emailTemplateResolver.setOrder(1);

return emailTemplateResolver;
}

@Bean
public ServletContextTemplateResolver defaultWebTemplateResolver(){
ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver();
webTemplateResolver.setPrefix("/templates/");
webTemplateResolver.setSuffix(".html");
webTemplateResolver.setTemplateMode("HTML5");
webTemplateResolver.setCharacterEncoding("UTF-8");
webTemplateResolver.setOrder(2);

return webTemplateResolver;
}

@Bean
public SpringTemplateEngine templateEngine(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(emailTemplateResolver());
templateEngine.setTemplateResolver(defaultWebTemplateResolver());
return templateEngine;
}

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();

thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");

return thymeleafViewResolver;
}
}


Folder and files tree is like:

src
main
resources
templates
login.html
error.html
mails
exampleMail.html


But it does not work. Application starts correctly but accessing pages (which works without this configuration) causes exception like:

Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers


I tried to put different prefixes into webTemplateResolver.setPrefix but without success.


Also I have noticed reported bug in earlier version of Thymeleaf causing that but looks like it was fixed and I have newer version.


Does anyone see the misteake in this configuration?

Answer

The main problem is that you are configuring too much.

Spring Boot already configures a TemplateEngine as well as a ThymeleafViewResolver. See the ThymeleafAutoConfiguration for that. If you look at that class you will also notice that it will auto detect any ITemplateResolver instances you might have in your application and it will add it to the SpringTemplateEngine.

The solution is quite simple remove everything but the email configuration and the emailTemplateResolver. Everything else will be handled by Spring Boot.

@Configuration
public ThymeleafEmailConfiguration {
   @Bean 
   public JavaMailSender getJavaMailSenderImpl(){
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

        Properties props = new Properties();
        /* some properties here */

        javaMailSender.setJavaMailProperties(props);

        return javaMailSender;
    }

    @Bean
    public ClassLoaderTemplateResolver emailTemplateResolver(){
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("/mails/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML5");
        emailTemplateResolver.setCharacterEncoding("UTF-8");
        emailTemplateResolver.setOrder(1);

        return emailTemplateResolver;
    }
}