2013-04-03 39 views
2

我有這樣的代碼來發送電子郵件:爲什麼此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" 

電子郵件以空主題和空收件人列表到達。 什麼會導致這種奇怪的行爲?

+1

您使用哪種類型的郵件發送API?沒有這些信息,很難有機會得到答案。 –

+0

我很驚訝你可以同時調用email.setHtmlMsg和email.setTextMsg。我會想到這個或那個。直接使用java.mail.api你可以指定多個部分 –

+0

http://stackoverflow.com/questions/5068827/how-do-i-send-html-email-via-java – tgkprog

回答

0

我看到你使用apache commons?

嘗試取出的頭部和身體標記

所以,你將有

email.setHtmlMsg("<html><p>Hello World!</p></html>"); 

另一件事,不應該有所作爲,但你可以嘗試是設置正確瓦魯爾足球俱樂部的 email.setHostName(「郵件.myserver.com「);

參考commons eml user guide

併發送郵件到Gmail帳戶,也許服務器的設置,只允許文字和刪除HTML?你可以發送相同的郵件編號HTML從Gmail(豐富的格式)?

+0

我在自己測試了相同的代碼包含的項目。它有效。 – mrt181

+0

當你從測試項目發送的服務器上的msg有額外的頭?它是同一版本的Apache公共?檢查類路徑,或者當它啓動你的應用程序和測試項目時重複使用java -verbose so java會告訴你每個包的加載位置 - 查看是否有相同的版本。 – tgkprog

+0

這是maven項目中的一個本地問題。當我在自己的Maven項目中僅使用apache-commons而不是它的工作。 – mrt181

相關問題