Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a 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 Thymeleaf 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
I 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 mistake in this configuration?

like image 460
Piotr Kończak Avatar asked Sep 01 '14 17:09

Piotr Kończak


People also ask

How do I enable Thymeleaf in spring boot?

Spring Boot will provide auto-configuration for Thymeleaf. Add spring-boot-starter-thymeleaf dependency in pom. xml to enable this auto-configuration. No other configurations required, Spring Boot will inject all required configuration to work with Thymeleaf.

What is better than Thymeleaf?

AngularJS, Vaadin, JSTL, Bootstrap, and React are the most popular alternatives and competitors to Thymeleaf.

Do people still use Thymeleaf?

While Thymeleaf is more of a template engine for server-side application development. But Thymeleaf's popularity is on a steady rise. The developer community is slowly moving away from 'once a common' MVC framework for Javascript-based development.


1 Answers

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 class 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;
    }
}
like image 101
M. Deinum Avatar answered Nov 13 '22 22:11

M. Deinum