为什么此 java 邮件以纯文本而不是 html 形式到达收件人?

2024-03-18

我有这个代码来发送电子邮件:

public static void sendHtmlTextWithPlainTextAlternative(final String to,
    final String from, final String subject, final String plainText,
    final String htmlText) throws MessagingException {

    final HtmlEmail email = new HtmlEmail();
    email.setHostName(SMTP);
    try {
        email.addTo(getStringAddresses(to));
        email.setFrom(from);
        email.setSubject(subject);
        email.setHtmlMsg("<html><head></head><body><p>Hello World!</p></body></html>");
        email.setTextMsg("Hello World!");
        email.send();
    } catch (final EmailException e) {
        e.printStackTrace();
    }
}

private static String[] getStringAddresses(final String to) {
    return to.split(" |,|;|\\r?\\n|\\r");
}

但我在电子邮件客户端(Outlook 2010)中收到的只是一条纯文本消息,我可以在其中看到 html 标记和替代纯文本或空白的富文本消息(Outlook 2002)。

这是摘录

------=_Part_0_756354128.1364993577885
Content-Type: multipart/alternative; boundary="----=_Part_1_48519531.1364993577890"

------=_Part_1_48519531.1364993577890
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello World!
------=_Part_1_48519531.1364993577890
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<html><head></head><body><p>Hello World!</p></body></html>
------=_Part_1_48519531.1364993577890--

------=_Part_0_756354128.1364993577885--

根据一位 Exchange Server 管理员的说法,该消息的开头应包含类似以下内容

0 2.1.5 Recipient OK
DATA
354 Start mail input; end with <CRLF>.<CRLF>

Content-Type: multipart/mixed; boundary="----=_Part_1_933059347.1364987366297"

但它是这样的(摘录):

250 2.1.5 Recipient OK
DATA
354 Start mail input; end with <CRLF>.<CRLF>

This is the content preamble.
------=_Part_1_933059347.1364987366297
Content-Type: multipart/alternative; boundary="----=_Part_0_1905186593.1364987366295"

电子邮件到达时主题和收件人列表都是空的。 什么可能导致这种奇怪的行为?


在找到要寻找的内容后,解决方案非常简单,我必须感谢 Cedric Champeau。这是与 geronimo-javamail 的冲突,该冲突是通过另一个 Maven 依赖项引入的。我所要做的就是排除这种依赖性:Apache CXF + Maven + Javamail + Log4J(更新) http://www.jroller.com/melix/entry/apache_cxf_maven_javamail_awful

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么此 java 邮件以纯文本而不是 html 形式到达收件人? 的相关文章

随机推荐