【问题标题】:JavaMail API: Maintain escape characters in mail content of type htmlJavaMail API:在 html 类型的邮件内容中维护转义字符
【发布时间】:2018-05-25 10:06:04
【问题描述】:

我有一个要求,我在表单字符串中接收用户输入,例如:

"Hi there,\r\n\r\nThe information is not visible in the page since you’ve adjusted the app.\r\n\r\ncan you please fix it?\r\n\r\nThanks,\r\n\r\nXYZ"

虽然它在 UI 中看起来像这样:

Hi there,

The information is not visible in the page since you’ve adjusted the app.

can you please fix it?

Thanks,

XYZ

我需要用/r /n 解析上面的字符串,然后通过邮件将其发送给其他人。

现在,当我在控制台中打印它时,它的打印效果就像它在 UI 中的样子,很明显。

但是,当我尝试将此字符串作为 java mail 内容时,当我检查收到的电子邮件时,我只是将所有内容放在一行中。

邮件的内容类型设置为text/html; charset=utf-8。我也尝试了pre-wrap 样式,但没有奏效。

private static String createEmailContent(String descp) {

    StringBuilder sb = new StringBuilder("<style> div { white-space: pre-wrap;}</style><div>");
    sb.append(descp);
    sb.append("/<div>");
    return sb.toString();
}

如何美化内容以使其看起来与收到的完全一致?

【问题讨论】:

  • 是文字代码吗?如果是这样,将/&lt;div&gt; 更正为&lt;/div&gt; 是否有效?如果不考虑编写代码插入&lt;br/&gt; 而不是\r\n
  • 不。没用。

标签: java jakarta-mail html-email


【解决方案1】:

这是纯文本内容,只需将其作为 text/plain 而不是 text/html 发送。

如果您真的需要将其作为 text/html 发送,则需要处理数据,而不是 html 换行符
或将其全部包装在

 中。
    

【讨论】:

    猜你喜欢
    • 2013-04-05
    • 2018-04-23
    • 2014-06-01
    • 1970-01-01
    • 2012-02-26
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多