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:


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>

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

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


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
spring.mail.username=sender email address
spring.mail.password=sender email password  # TLS , port 587

Send email

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

private JavaMailSender javaMailSender;

new Thread(new Runnable() {
    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);

        } catch (MessagingException e) {


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 {


    } catch (Exception e) {

    return content.toString();

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