Sending HTML Email in Java Spring boot

Recently was refactoring a Java Spring boot project which I bought in Codecanyon. It had an API endpoint to send a HTML verification code. The HTML was hard coded inside the Java code and couldn’t be formatted easily because of this. So I planned to use https://www.thymeleaf.org/ as the templating engine. Here is the step by step for anyone going to do similar thing using Java Sprint boot.

Step 1

Add the maven dependency for the library in pom.xml file as shown below

........... 
       <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>
.......

Step 2

Create a Java class to generate the HTML using the template.

public class Template {
    public static String getVerificationEmail(String code){
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setSuffix(".html");
        resolver.setPrefix("templates/");
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);
        final Context context = new Context(Locale.ENGLISH);
        context.setVariable("verificationCode", code);
        final String html = templateEngine.process("email-verification", context);
        return html;
    }
}

Here I am passing a single variable called verificationCode but you can pass as many as you want. Also we refer the path “templates/” and we need to place the email templates in the directory. Also we refer to the file name “email-verification” with the extension “.html”

Step 3

Create a directory named “template” under /src/main/resources and create a file with name “email-verification.html”. The HTML can refer the variable verification code as shown below. Please note all the expressions needs to be in [[….]] so that it will be executed and replaced with actual values.

...........
............
                             <tr>
                                <td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;">
                                    <div style="font-family:open Sans Helvetica, Arial, sans-serif;font-size:24px;font-weight:bold;line-height:1;text-align:center;color:#000000;">[[${verificationCode}]]</div>
                                </td>
                            </tr>
.............
.............

Hope this quick steps helped you in your project.

Nish

Full stack developer, and Pega Certified Lead System architect since 2013 and with nearly 16 years experience in Pega. Connect with me in Linked in at https://www.linkedin.com/in/msnish/

One thought on “Sending HTML Email in Java Spring boot

  1. Hi I have read a lot from this blog thank you for sharing this information. We provide all the essential courses for professionals like Data Science Course In Jaipur , Full stack Developer, data structures courses, System design course , Python, AI and Machine Learning, Tableau, etc. for more information log in to our website https://skillslash.com/system-design-course

Leave a Reply

Your email address will not be published. Required fields are marked *