Spring boot email template with Thymeleaf

Nuwantha Fernando
4 min readOct 15, 2020

Through this article, you are about to learn how to use Thymeleaf to create templates using Html with Spring boot. And also we will integrate this with Smtp to configure a simple but powerful email system. Before move to the topic, let's have some simple ideas about Thymeleaf. As to the Thymeleaf official website it introduce Thymeleaf as follows.

Thymeleaf is a modern server-side Java template engine for both web and standalone environments. Thymeleaf’s main goal is to bring elegant natural templates to your development workflow — HTML that can be correctly displayed in browsers and also work as static prototypes, allowing for stronger collaboration in development teams. With modules for Spring Framework, a host of integrations with your favorite tools, and the ability to plug in your own functionality, Thymeleaf is ideal for modern-day HTML5 JVM web development — although there is much more it can do.

So Let’s Start..

Before starting the tutorial you should have to make sure your machine is set up with Java. For this tutorial, I'm using Java 11.

Create new project

First of all, you should have to initialize a spring boot project from spring Initializer. I’m not going to add dependencies at the initialization. So we can insert them later. Below will be a screenshot of the initialization page for your reference.


Step 1 :

Open up the downloaded project with your favorite IDE. For this tutorial, I’m using IntelliJ Idea.

Let’s start the implementation by adding the required dependencies. I have created a maven based project. So, I’m adding the required dependencies to the root level pom.xml file. You can easily find the required dependencies from the maven repository. For this tutorial, we required the following dependencies.

spring-boot-starter-mail — Starter for using Java Mail and Spring Framework’s email sending support

thymeleaf-spring5 —Modern server-side Java template engine for both web and standalone environments

thymeleaf-layout-dialect -A dialect for Thymeleaf that allows you to use layout/decorator templates to style your content.

org.projectlombok-Spice up your java: Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!

Below will be the complete pom.xml content of the final project.

Step 2 :

Now I’m going to add the required properties to the application.properties file. You are free to use application.yml as well. But for this tutorial, 'm using application.properties. For this tutorial, we required the following properties.

Step 3 :

Let’s add spring thmeleaf configurations. Create new package as configs. And create new class as ThymeleafTemplateConfig.java inside the created package. And add below code line to the newly created class.

In order to process our templates, we will configure a SpringTemplateEngine which specially configured for email processing, in our Spring boot email template configuration. We need to tell Thymeleaf where the email templates are located. We do this by creating and configuring the ClassLoaderTemplateResolver We can set a prefix and suffix to configure where thymeleaf will search for the HTML email templates.

Step 4 :

Using Thymeleaf for processing our email templates would allow us to use some interesting features:

  1. Expressions in Spring EL.
  2. Flow control: iterations, conditionals, etc.
  3. Utility functions: date/number formatting, dealing with lists, arrays…
  4. Easy i18n, integrated with our application’s Spring internationalization infrastructure.
  5. Natural templating: our email templates can be static prototypes, written by UI designers.

Also, given the fact that Thymeleaf has no required dependencies on the servlet API, there would be no need at all to create or send email from a web application. In this example, we are going to create a very simple HTML template.

We should have to place the HTML template inside the templates. Below will be the sample template which I used for this tutorial.

Step 5 :

Now I’m going to create the EmailSenderService to send emails. Create new package as service and place the EmailSenderService.java class inside the package. Below will be the implementation of the service class.

Step 6 :

Let’s test the application. For this tutorial I'm using SprinBootTest class inorder to test the implementation.

By running the test class, Hopefully you will receive a email which created using thmeleaf.

Finally here is the source code for the tutorial. And write a response if you are having an issue implementing the thmeleaf email templates in your projects.



Nuwantha Fernando

BSc (Hons) in Computer Science and Software Engineering | Software Engineer at Treinetic (pvt) Ltd