Integrating Spring Boot with Thymeleaf for Email Templates

Email notifications are an essential feature of many applications, allowing for user engagement and communication. Using Thymeleaf with Spring Boot, you can create dynamic email templates that are easy to manage and customize. This post will guide you through integrating Thymeleaf for building email templates within your Spring Boot application.

What is Thymeleaf?

Thymeleaf is a Java-based templating engine that allows you to create dynamic web pages and templates. It is particularly well-suited for server-side rendering, making it an excellent choice for generating HTML emails. Thymeleaf offers:

  • Natural templating: You can open the Thymeleaf templates in a browser directly as static HTML files.
  • Separation of concerns: Helps in keeping presentation logic decoupled from business logic.

Setting Up Thymeleaf for Email Templates

To get started, you will create a Spring Boot application with Thymeleaf configured for email purposes.

1. Create a Spring Boot Project

Use Spring Initializr to bootstrap your Spring Boot project, including the Thymeleaf dependency:

  • Spring Web
  • Spring Boot Starter Thymeleaf

2. Adding Dependencies

Add the Thymeleaf dependency in your pom.xml if you haven’t added it through Spring Initializr:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3. Configuring Email Properties

You will need to configure your email server settings in application.properties. Here’s an example configuration for using Gmail:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your_email@gmail.com
spring.mail.password=your_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Creating Thymeleaf Email Templates

Create a directory called templates under src/main/resources. Inside this directory, create an email template file named welcome-email.html with the following content:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Welcome to Our Service</title>
</head>
<body>
    <h1>Welcome, <span th:text="${user.name}"></span>!</h1>
    <p>Thank you for joining us. Please confirm your email address by clicking the link below.</p>
    <a th:href="${confirmationUrl}">Confirm Your Email</a>
</body>
</html>

Service Class for Sending Emails

Next, create a service class that will configure the process of sending emails using the Thymeleaf template:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender emailSender;

    @Autowired
    private TemplateEngine templateEngine;

    public void sendWelcomeEmail(User user, String confirmationUrl) {
        Context context = new Context();
        context.setVariable("user", user);
        context.setVariable("confirmationUrl", confirmationUrl);
        String htmlContent = templateEngine.process("welcome-email", context);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(user.getEmail());
        message.setSubject("Welcome to Our Service!");
        message.setText(htmlContent);
        message.setFrom("your_email@gmail.com");

        emailSender.send(message);
    }
}

Using the Email Service

Inject the EmailService into your controller or service where you handle user registration or related tasks, allowing you to send email notifications:

@Autowired
private EmailService emailService;

// Inside a method where user registration happens
emailService.sendWelcomeEmail(newUser, confirmationUrl);

Conclusion

Integrating Thymeleaf for email templates in your Spring Boot application enables you to create dynamic, engaging emails that improve user interaction. By leveraging Thymeleaf’s template engine, you can manage email content effectively, ensuring messages are personalized and relevant to each user.

For deeper learning about advanced email features and further integration with Spring Boot, check out the comprehensive resources provided by ITER Academy to enhance your Spring development skills.

To learn more about ITER Academy, visit our website.

Scroll to Top