简体   繁体   中英

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. 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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