How to send email in Spring boot using Freemarker

Sending email is important part in web app development. It helps you to grow your business. This tutorial is about spring boot email template example. We will learn how to send email in spring boot using freemarker.

Mail and Freemarker library

To get started make sure that your project contains these library below:

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

Freemarker html template

We will use a html template to send the email. You have to put the template file in to the resources/templates folder. Make sure the file extension is .flth. Here fmo-welcome-email.flth as example:

<!DOCTYPE html>
<html>
<body>

<h1>Hi ${firstName}</h1>

<p>Welcome to ${appName}</p>

</body>
</html>

This is a very basic and ugly looking email template ever. But it’s enough to learn how it works. You see the html code contains ${firstName} and ${appName} variable. You have to send values from java when you send the email.

Email configuration

In this tutorial we will be using gmail smtp server. Here is the configurations. Paste these codes into your application.properties

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=sender email address
spring.mail.password=sender email password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000  # TLS , port 587 
spring.mail.properties.mail.smtp.starttls.enable=true

Send email

We are almost done. Here is the final part. Execute the code below in order to send the email.


@Autowired
private JavaMailSender javaMailSender;

new Thread(new Runnable() {
    @Override
    public void run() {

        Map<String, String> usersInfo = new HashMap();
        usersInfo.put("userName", "Your user's name");

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        try {

           MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
           mimeMessageHelper.setTo("User's email address");
           mimeMessageHelper.setFrom("Sender's email address");
           mimeMessageHelper.setSubject("Subject here");
           mimeMessageHelper.setText(geContentFromTemplate(usersInfo), true);
           javaMailSender.send(mimeMessage);

        } catch (MessagingException e) {
            System.out.println(e.toString());
        }

    }
}).start();

public String geContentFromTemplate(Map<String, String> usersInfo) {
    
    StringBuffer content = new StringBuffer();
    Map<String, Object> model = new HashMap<>();
    model.put("firstName", usersInfo.get("userName"));
    model.put("appName", "Name of your app");

    try {

        content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
                      fmConfiguration.getTemplate("fmo-welcome-email.flth"), 
                      model
        ));

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

    return content.toString();
}

And you are done. This how you can send email in Spring Boot application. It’s very easy and quick.