春季电子邮件和换行符
有人可以告诉我如何在电子邮件内容中插入换行符。我使用此代码片段发送电子邮件。
public boolean sendMail(final Account player, final Object tl, final String type)
{
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception
{
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
String msgAdmin = msgFrom;
message.setTo(player.getEmail()); // TODO: changed from msgAdmin to player.getEmail()
message.setFrom(msgFrom);
message.setSubject(type + " invitation");
Map model = new HashMap();
model.put("tl", tl);
model.put("player", player);
model.put("type", type);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
"com/test/mail/invite.vm", model);
logger.debug(text);
message.setText(text, true);
}
};
return sendMail(preparator);
}
我尝试了电子邮件内容中的 \r\n 字符。但这似乎不起作用。像 BR 标签这样的 HTML 标记可以工作,但我不想在电子邮件内容中添加 html 标记。还有其他解决方案吗?
Can someone tell me how to insert newline characters in email content. I use this code snippet to send emails.
public boolean sendMail(final Account player, final Object tl, final String type)
{
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception
{
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
String msgAdmin = msgFrom;
message.setTo(player.getEmail()); // TODO: changed from msgAdmin to player.getEmail()
message.setFrom(msgFrom);
message.setSubject(type + " invitation");
Map model = new HashMap();
model.put("tl", tl);
model.put("player", player);
model.put("type", type);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
"com/test/mail/invite.vm", model);
logger.debug(text);
message.setText(text, true);
}
};
return sendMail(preparator);
}
I tried \r\n characters in the email content. But it doesn't seem to work. HTML markup like BR tag works, but i dont want to add html markups in the email content. Any other solution is possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,问题是当您调用 message.setText 时,您将第二个参数设置为 true。这意味着该消息被解释为 HTML。为了显示电子邮件换行符,只需将第二个参数设置为 false。
Actually the problem is when you are invoking the message.setText, you are setting the second arguement to true. Which means the message is interpreted as HTML. In order for the emails newlines to show, just set that second argument to false.
换行符和速度模板是一个有据可查的问题。最好的解决方法是将“\n”存储为可供模板使用的属性值。然后引用该属性。
Newline characters and velocity templates is a well-documented problem. The best workaround is to stash "\n" as a value of a property that you make available to template. Then reference that property.