有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringVelocity模板电子邮件?

我有下面的速度模板

<!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) 个答案

  1. # 1 楼答案

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