Search code examples
javahtmlspring-bootthymeleaf

Rich HTML email in Spring with Thymeleaf


I use Spring thymeleaf to send rich email templates.

I use the below java and html code. Not getting any error but the $ values are not getting replaces with the context values. I get the raw html as it is in the email. Am I missing anything else here?

Java code :

final Context ctx = new Context();
ctx.setVariable("subject", "Welcome to App");
ctx.setVariable("name", fullName);
ctx.setVariable("userEmailId", email);
ctx.setVariable("activateLink", agentAddedUrl);
ctx.setVariable("dialrSupportEmail", dialrSupportEmail);
ctx.setVariable("dialerSupportNumber", dialrSupportPhone);

final String htmlContent = templateEngine.process("agentEmailTemplate.html", ctx);

System.out.println("HTML ==>>> "+htmlContent); 

HTML, agentEmailTemplate.html,

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title th:text="${subject}">Welcome to TestApp</title>
<style type="text/css">
</style>
<script type="colorScheme" class="swatch active">
    </script>
</head>
<body paddingwidth="0" paddingheight="0"
    style="padding-top: 0; padding-bottom: 0; padding-top: 0; padding-bottom: 0; background-repeat: repeat; width: 100% !important; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; -webkit-font-smoothing: antialiased;"
    offset="0" toppadding="0" leftpadding="0">
    <table width="100%" border="0" cellspacing="0" cellpadding="0"
        class="tableContent bgBody" align="center"
        style='font-family: Helvetica, Arial, serif;'>

        <div class='movableContent'>
            <table width="520" border="0" cellspacing="0" cellpadding="0"
                align="center">
                <tr>
                    <td align='left'>
                        <div class="contentEditableContainer contentTextEditable">
                            <div class="contentEditable" align='center'>
                                <p
                                    style='text-align: left; color: #222222; font-size: 14px; font-weight: normal; line-height: 19px;'>

                                    <br> Hi ${name},</br> <br> Your App Admin has
                                    created an account for you.</br> <br> To sign up for
                                    ContactCentral Account Agent Access,click here and create your
                                    own password .Your username is ${userEmailId} . </br> <br> <br>
                                    Have questions? Get in touch with us via ${dialrSupportEmail}
                                    or call ${dialerSupportNumber}.</br>
                                    </br> <br> <br> Cheers,</br>
                                    </br> <br> <span style='color: #222222;'>App Accounts
                                        Team</span></br>
                                </p>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </table>
</body>
</html>

Solution

  • In Thymeleaf you are suppose to use a th:text inside the html tag to print variable value

    Refer this link