Spring Boot

Create Email in Spring Boot using Apache FreeMarker

Suppose our application needs to email in text or HTML format; how can we do that without changing a lot of codes? We use templates! Spring Boot makes it a lot easier working with Apache FreeMarker with FreeMarker Starter POMs. This quickstart post shows how to configure and use FreeMarker with Spring Boot. We know applications can exchange information in various formats, but the data may be the same. For instance, we want to send emails in either text or HTML format, although the content may be the same in either form.

About Template Engines like Apache FreeMarker

Template engines like Apache FreeMarker and Thymeleaf allow applications to format output quickly using templates. These engines use templates that dictate how to display the data. They take these templates, map data to specific locations, and fill them in before generating the final output.

Spring Boot And Apache FreeMarker Starter POM

To use FreeMarker in Spring Boot, we can use the following starter POM.

Generate Email Using Template File

By default, Spring Boot looks for the template files in the /resources/templates directory. To generate an email from a template, we need to craft and use a  .flth file. Suppose we have the following template content, which is essentially an HTML with a FreeMarker-specific placeholder ${name}.

Then, we can use the following codes anywhere in our codebase.

Note the Map instance we pass to the processTemplateIntoString method. The map has an entry with a key name whose value is turreta.com. Also, we use the same key name as a variable ${name} within the Apache FreeMarker template file.

If we put these codes in a controller, we will have something similar as follows.

To help Apache FreeMarker read the template into memory, we use the getTemplate method from the FreeMarker Configuration class. When we access the endpoint, we will get the following output on the browser.

We tested the codes using Spring Boot 2.5.6.

Got comments, or suggestions? Please visit our Facebook page!

You Might Also Like