我有這樣的代碼來發送電子郵件:爲什麼此java郵件以純文本而不是html作爲收件人?
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服務器管理員的消息應該包含這樣的事情在一開始
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"
電子郵件以空主題和空收件人列表到達。 什麼會導致這種奇怪的行爲?
您使用哪種類型的郵件發送API?沒有這些信息,很難有機會得到答案。 –
我很驚訝你可以同時調用email.setHtmlMsg和email.setTextMsg。我會想到這個或那個。直接使用java.mail.api你可以指定多個部分 –
http://stackoverflow.com/questions/5068827/how-do-i-send-html-email-via-java – tgkprog