2012-12-19 64 views
2

我試圖通過GMail發送電子郵件(實際上是Google Apps)。這對於正常的消息來說是完美的,但我當然必須測試極端情況。Javamail/email commons:空消息給出奇怪的錯誤

現在我試圖發送一條消息沒有身體,我收到一個奇怪的錯誤消息。因爲我想消除代碼中的錯誤,所以我想知道我是在做錯什麼或者我正在使用的庫中有錯誤。

HtmlEmail currentMessage = new HtmlEmail(); 
currentMessage.setSSL(true); 
currentMessage.setSslSmtpPort(465); 
currentMessage.setHostName("smtp.gmail.com"); 
currentMessage.setAuthentication("[email protected]", "secret"); 

/** rs is a ResultSet from database **/ 

if(rs.getString("mailFrom") != null && !rs.getString("mailFrom").isEmpty()) 
    currentMessage.setFrom(rs.getString("mailFrom")); 
else 
    currentMessage.setFrom("[email protected]"); 
currentMessage.setTo(this.convertRecipientStringToArray(rs.getString("mailTo"))); 
currentMessage.setCc(this.convertRecipientStringToArray(rs.getString("mailCC"))); 
currentMessage.setBcc(this.convertRecipientStringToArray(rs.getString("mailBC"))); 
currentMessage.setSubject(rs.getString("mailSubject")); 
if(rs.getString("mailBody") != null && !rs.getString("mailBody").isEmpty()) 
    currentMessage.setTextMsg(rs.getString("mailBody")); 
if(rs.getString("mailHtmlBody") != null && !rs.getString("mailHtmlBody").isEmpty()) 
    currentMessage.setHtmlMsg(rs.getString("mailHtmlBody")); 
if(rs.getString("mailReplyTo") != null && !rs.getString("mailReplyTo").isEmpty()) 
    currentMessage.addReplyTo(rs.getString("mailReplyTo")); 
else 
    currentMessage.addReplyTo("[email protected]"); 
currentMessage.send(); 

此代碼爲 「正常」 的郵件:一個有效的機構,主題,收件人等

當兩個mailBodymailHtmlBodyNULL或空的DATABSE ,我得到以下錯誤:

Sending the email to the following server failed : smtp.gmail.com:25

完整的錯誤日誌(自己的日誌格式) /堆棧跟蹤:

[CRITICAL] 2012-12-19 17:08:00 [CET] - Exception occurred in function com.mypackage.mymailobject.outgoing_sendMails: Sending the email to the following server failed : smtp.gmail.com:25 

Stack trace: 
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:25 
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242) 
    at org.apache.commons.mail.Email.send(Email.java:1267) 
    at com.mypackage.mymailobject.outgoing_sendMails(mymailobject.java:85) 
    at com.mypackage.mymailobject.outgoing_do(mymailobject.java:69) 
    at com.mypackage.Mailer.main(Mailer.java:132) 
Caused by: javax.mail.MessagingException: IOException while sending message; 
    nested exception is: 
    java.io.IOException: javax.mail.MessagingException: Empty multipart: multipart/mixed; 
    boundary="----=_Part_0_916488860.1355933279096" 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) 
    at javax.mail.Transport.send0(Transport.java:195) 
    at javax.mail.Transport.send(Transport.java:124) 
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232) 
    ... 4 more 
Caused by: java.io.IOException: javax.mail.MessagingException: Empty multipart: multipart/mixed; 
    boundary="----=_Part_0_916488860.1355933279096" 
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105) 
    at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) 
    at javax.activation.DataHandler.writeTo(Unknown Source) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476) 
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099) 
    ... 7 more 

我現在有2個問題:

  1. 它應該是可以發送電子郵件沒有身體?這是允許的嗎?
  2. 爲什麼錯誤消息端口號是25而不是465?
+0

您是否嘗試過,如果將消息設置爲空字符串有效?此外,是完整的錯誤消息,沒有堆棧跟蹤或嵌套異常? –

+0

如果我將消息設置爲空字符串,我會從Apache Commons Email中收到另一個異常。在http://commons.apache.org/email/xref/org/apache/commons/mail/HtmlEmail.html查看第131行 – bartlaarhoven

+0

將堆棧跟蹤添加到原始帖子。 :)這有幫助嗎? – bartlaarhoven

回答

3

儘管我找不到它的文檔,但經驗證明顯示,Apache Commons Email HtmlEmail對象中不允許空的body部分。文本消息或html消息應設置爲非空字符串。

帶有端口25的錯誤消息是Apache Commons Email的代碼中的錯誤,它在打印出smtp端口時未考慮到SSL。爲此,錯誤報告已經在https://issues.apache.org/jira/browse/EMAIL-123

-1

要發送電子郵件與gmail,認爲你應該啓用傳輸層安全也。

currentMessage.setTLS(true); 
+0

正如我所說,發送郵件完全符合此代碼,所以不是這樣。這只是郵件正文爲空的特殊情況。此外,我使用SSL而不是TLS。 – bartlaarhoven

4

Should it be possible to send an email without a body? Is that allowed?

是提交RFC 5322特別允許電子郵件消息有一個空體:

"A message consists of header fields, optionally followed by a message body."

這裏的BNF:

body   = (*(*998text CRLF) *998text)/obs-body 

http://tools.ietf.org/html/rfc5322#section-3.5

5

MIME規範不允許多部分內容與沒有身體部位。 JavaMail API(我認爲版本爲1.5)允許您將系統屬性mail.mime.multipart.allowempty設置爲true以解決此問題。

0

我有完全相同的例外,但最終成爲一個愚蠢的錯誤,我忘了將消息的主體添加到多部分消息。因此錯誤!

BodyPart body = new MimeBodyPart(); 
body.setText(("Message Body")); 
multipart.addBodyPart(body); // ** here 
message.setContent(multipart); 
Transport.send(message);