【问题标题】:Newline character is not interpreted correctly in MimeMessage?在 MimeMessage 中没有正确解释换行符?
【发布时间】:2012-11-01 17:59:38
【问题描述】:
StringBuffer emailMessage = new StringBuffer("Dear Scott");
emailMessage.append("\r\n");
emailMessage.append("Sending mail over internet");

所以这是我在调试器中检查时的格式化内容

Dear Scott,
Sending mail over internet

但是当我在 Thunderbird 中收到它时,我会在一行中收到完整的消息,如下所示。不知何故换行符 将内容作为 html 发送时未正确解释

 Dear Scott,Sending mail over internet

这是我将消息作为 html 发送的方式

 MimeMessage msg = null;
 msg = new MimeMessage(mailSession);
 msg.setText(body, CHARSET_UTF_8, "html");

如果我只是将文本作为 msg.setText(body, CHARSET_UTF_8) 发送,那么我会看到正确格式的消息,即“通过 Internet 发送邮件” 下一行。 我不明白为什么将文本作为 html 发送时换行符没有被正确解释?

【问题讨论】:

    标签: java mime-types mime mime-message


    【解决方案1】:

    因为您要发送 HTML 电子邮件,所以您必须使用 <br /> 而不是 \r\n,就像 经典 HTML 文档(例如 .html 文件)一样。当您使用\r\n 时,呈现的内容不会打印新行,但邮件的源代码会(反之亦然)。

    【讨论】:

      【解决方案2】:

      HTML 不解释换行符。当您将内容类型设置为“html”时,请使用“
      ”而不是“\r\n”:

      StringBuffer emailMessage = new StringBuffer("Dear Scott");
      emailMessage.append("<br/>");
      emailMessage.append("Sending mail over internet");
      

      【讨论】:

        猜你喜欢
        • 2015-08-28
        • 2014-07-18
        • 2022-12-03
        • 1970-01-01
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        相关资源
        最近更新 更多