我有以下速度模板。
<!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/>
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} |
-----------------------------
在上面的邮件中,为什么占位符没有被替换为值?请帮我。