java MimeMessage 中的换行符未正确解释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13358504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Newline character is not interpreted correctly in MimeMessage?
提问by M Sach
StringBuffer emailMessage = new StringBuffer("Dear Scott");
emailMessage.append("\r\n");
emailMessage.append("Sending mail over internet");
So here is my formatted content when i inspect in debugger
所以这是我在调试器中检查时的格式化内容
Dear Scott,
Sending mail over internet
But when i receive it in thunderbird, i receive complete message in one line like below. Somehow newline character is not interpreted correctly when sending the content as html
但是当我在雷鸟中收到它时,我会在一行中收到完整的消息,如下所示。以 html 格式发送内容时,换行符无法正确解释
Dear Scott,Sending mail over internet
here is how i am sending the message as html
这是我如何将消息作为 html 发送
MimeMessage msg = null;
msg = new MimeMessage(mailSession);
msg.setText(body, CHARSET_UTF_8, "html");
If i simply send the text as msg.setText(body, CHARSET_UTF_8) then i see the message in right format i.e "Sending mail over internet" in seen in next line. I am not getting why new line character is not interpreted correctly when sending the text as html?
如果我只是将文本作为 msg.setText(body, CHARSET_UTF_8) 发送,那么我会看到正确格式的消息,即“通过互联网发送邮件”在下一行中看到。我不明白为什么在将文本作为 html 发送时没有正确解释换行符?
回答by sp00m
Because you're sending an HTML email, you have to use <br />
instead of \r\n
, just like a classicHTML document (e.g. a .html
file). When you use \r\n
, the rendered content won't print a new line, but the source code of the mail will (and vice versa).
因为您要发送 HTML 电子邮件,所以您必须使用<br />
代替\r\n
,就像经典的HTML 文档(例如.html
文件)一样。当您使用 时 \r\n
,呈现的内容不会打印新行,但邮件的源代码会(反之亦然)。
回答by David Tanzer
HTML does not interpret newline characters. When you set the content type to "html", use "<br/>" instead of "\r\n":
HTML 不解释换行符。当您将内容类型设置为“html”时,请使用“<br/>”而不是“\r\n”:
StringBuffer emailMessage = new StringBuffer("Dear Scott");
emailMessage.append("<br/>");
emailMessage.append("Sending mail over internet");