简体   繁体   English

春季电子邮件和换行符

[英]Spring email and new line character

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. 我在电子邮件内容中尝试了\\ r \\ n个字符。 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. 像BR标签一样的HTML标记有效,但是我不想在电子邮件内容中添加html标记。 Any other solution is possible? 还有其他解决方案吗?

Actually the problem is when you are invoking the message.setText, you are setting the second arguement to true. 实际上,问题是当您调用message.setText时,您将第二个论点设置为true。 Which means the message is interpreted as HTML. 这意味着消息被解释为HTML。 In order for the emails newlines to show, just set that second argument to false. 为了显示电子邮件的换行符,只需将第二个参数设置为false。

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. 最好的解决方法是将“ \\ n”存储为可用于模板的属性的值。 Then reference that property. 然后引用该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM