Thymeleaf在Spring中提供丰富的HTML电子邮件

时间:2016-03-03 06:49:09

标签: java html spring-boot thymeleaf

我使用Spring百万美元来发送丰富的电子邮件模板。

我使用下面的java和html代码。没有得到任何错误,但$值没有取代上下文值。我得到了电子邮件中的原始html。我在这里错过了什么吗?

Java代码:

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>

2 个答案:

答案 0 :(得分:1)

在Thymeleaf中,您可以使用html标记内的th:文本来打印变量值

请参阅此link

答案 1 :(得分:0)

正如@ shakeel-osmani所示,添加变量的方式是错误的。

您应将th:text标签添加到HTML标签中,如下所示:

<p th:text="${variable}">Default body!</p>

th:text评估变量的值,该值将放置在标记的主体内。因此它将呈现为

<p>{{variable}}</p>

如果未设置该变量,那么在我们的示例中,默认正文Default body!将用作<p>标签的内容。