为什么这个 java 邮件以纯文本而不是 html 的形式到达收件人? [英] Why does this java mail arrive as plain text instead of html at recipient?

查看:27
本文介绍了为什么这个 java 邮件以纯文本而不是 html 的形式到达收件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有发送电子邮件的代码:

I have this code to send an email:

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

But all i get in my email client (Outlook 2010) is a plain text message where I can see the html markup and the alternative plain text or a rich text message that is blank (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 管理员的说法,该消息的开头应包含类似内容

According to one Exchange Server admin the message should contain something like this at the beginning

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

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

但它是这样到达的(摘录):

But it arrives like this (excerpt):

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"

收到的电子邮件主题为空,收件人列表也为空.什么可能导致这种奇怪的行为?

The email arrives with an empty subject and an empty recipient list. What could cause this strange behavior?

推荐答案

在找到解决方案后,我要感谢 Cedric Champeau.这是与通过另一个 maven 依赖项引入的 geronimo-javamail 的冲突.我所要做的就是排除该依赖项:Apache CXF + Maven + Javamail + Log4J(更新)

After finding out what to look for the solution was quite simple and I have to thank Cedric Champeau. It was a conflict with geronimo-javamail that was pulled in through another maven dependency. All I had to do was to exclude that dependency: Apache CXF + Maven + Javamail + Log4J (update)

这篇关于为什么这个 java 邮件以纯文本而不是 html 的形式到达收件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆