弹簧速度模板邮件?

时间:2015-08-31 18:33:34

标签: java spring email spring-mvc apache-velocity

我有低于速度模板。

<!DOCTYPE html>
<html>
<head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
</style>
</head>
<body>

<h4>Dear User</h4><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Your Instance has been created Successfully.<br/>
Please find attached <b>.pem</b> file which is required to login to your machine.<br/>

<h2>Machine Details:</h2>

<table style="width:100%">
  <tr>
    <th>Public IP:</th>
    <td>${publicIP}</td>
  </tr>
  <tr>
    <th>Public DNS:</th>
    <td>${publicDns}</td>
  </tr>
  <tr>
    <th>User Name:</th>
    <td>${userName}</td>
  </tr>
</table>

</body>
</html>
<br/>

发送电子邮件的代码:

public void sendMail(MailParams params) throws MessagingException {

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(params.getMailFrom());
        helper.setTo(params.getMailTo());
        helper.setSubject(params.getMailSubject());
        Template template = velocityEngine.getTemplate("./templates/"
                + params.getTemplateName());

        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("publicIP", params.getPublicIP());
        velocityContext.put("publicDns", params.getPublicDns());
        velocityContext.put("userName", params.getUserName());

        StringWriter stringWriter = new StringWriter();

        template.merge(velocityContext, stringWriter);

        //helper.setText(stringWriter.toString());
        message .setContent(stringWriter.toString(), "text/html");
        mailSender.send(message);
    }

收到邮件:

Dear User

       Your Instance has been created Successfully.
Please find attached .pem file which is required to login to your machine.
Machine Details:
-----------------------------
|Public IP: |${publicIP}     |
|Public DNS:    |${publicDns}|
|User Name: |${userName}     |
-----------------------------

在上述邮件中,为什么占位符不会被值替换?请帮帮我。

1 个答案:

答案 0 :(得分:-1)

我忘了为对象中的那些字段设置值。