2014-01-22 85 views
0

我在JavaMail中遇到了有關IMAP的奇怪問題。JavaMail IMAP郵件內容SSL/NON SSL

我已經知道很多關於JavaMail,我也知道在哪裏尋找問題/答案,但我找不到這個問題的解決方案。

我有IMAP頻道,我可以在其上設置啓用或禁用SSL。 當我創建新的電子郵件(例如,主題:「TEST」,正文:「HELLO」)並從Thunderbird客戶端發送,以便能夠使用我的IMAP頻道下載它時,情況變得很奇怪。

在SSL啓用上,一切正常。 (在我的IMAP頻道),我從電子郵件文件夾中獲得消息,成功連接到商店後,然後我從此消息getContent()(它始終是字符串類型,而不是MimeMultipart或多部分),並且輸出爲「HELLO」。

但是,在SSL DISABLED上,一切都正常,但我收到不同的身體。它看起來像整個消息解析(看下面)

message.getContent()。toString();使用SSL

HELLO 

message.getContent()。toString();不使用SSL

Return-Path: [email protected] 
Received: from [127.0.0.1] (localhost [127.0.0.1]) by PC ; Wed, 22 Jan 2014 13:23:17 +0100 
Message-ID: <[email protected]> 
Date: Wed, 22 Jan 2014 13:23:17 +0100 
From: "[email protected]" <[email protected]> 
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 
MIME-Version: 1.0 
To: [email protected] 
Subject: TEST 
Content-Type: text/plain; charset=UTF-8; format=flowed 
Content-Transfer-Encoding: 7bit 

有沒有人有任何想法是怎麼回事?下面是我的代碼片段(向您展示最重要的部分:

// properties used to create session 
String protocol = useSSL ? "imaps" : imap"; 

properties.setProperty("mail.store.protocol", protocol); 
properties.setProperty("mail." + protocol + ".host", ...); 
properties.setProperty("mail." + protocol + ".user", ...); 
properties.setProperty("mail." + protocol + ".port", ...); 
properties.setProperty("mail." + protocol + ".password", ...); 

// only when using ssl 
properties.setProperty("mail.imaps.auth", "true"); 
properties.setProperty("mail.imaps.starttls.enable", "true"); 
properties.setProperty("mail.imaps.ssl.checkserveridentity", "true"); 
properties.setProperty("javax.net.ssl.trustStore", ...); 
properties.setProperty("javax.net.ssl.trustStorePassword", ...); 

// get store and other important things 
Store store = this.session.getStore(protocol); 
Folder folder = store.getFolder("INBOX"); 
folder.open(Folder.READ_WRITE); 
Message[] newMessages = folder.getMessages(); 
for (Message newMessage : newMessages) { 
    Object o = newMessage.getContent(); 
    System.out(o.toString()); // should write "HELLO", but instead it parses whole message (only on NON SSL MODE!) 
} 

//編輯:加調試信息 //編輯2:調試信息包含使用{CONTENT_INFO,旗幟獲取的文件夾,現在全部細節,信封}

DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc] 
DEBUG: mail.imap.fetchsize: 16384 
DEBUG: protocolConnect returning false, host=127.0.0.1, [email protected], password=<null> 
* OK IMAPrev1 
A0 CAPABILITY 
* CAPABILITY IMAP4 IMAP4rev1 CHILDREN IDLE QUOTA SORT ACL NAMESPACE RIGHTS=texk 
A0 OK CAPABILITY completed 
DEBUG: protocolConnect login, host=127.0.0.1, [email protected], password=<non-null> 
A1 LOGIN [email protected] password 
A1 OK LOGIN completed 
DEBUG: connection available -- size: 1 
A2 SELECT INBOX 
* 1 EXISTS 
* 0 RECENT 
* FLAGS (\Deleted \Seen \Draft \Answered \Flagged) 
* OK [UIDVALIDITY 1378802425] current uidvalidity 
* OK [UNSEEN 50167] unseen messages 
* OK [UIDNEXT 50168] next uid 
* OK [PERMANENTFLAGS (\Deleted \Seen \Draft \Answered \Flagged)] limited 
A2 OK [READ-WRITE] SELECT completed 
A3 SEARCH UNSEEN ALL 
* SEARCH 1 
A3 OK Search completed 
A4 FETCH 1 (BODYSTRUCTURE) 
* 1 FETCH (UID 50167 BODYSTRUCTURE ("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL "test" "7bit" 7 2)) 
A4 OK FETCH completed 
A5 FETCH 1 (BODY[TEXT]<0.7>) 
* 1 FETCH (UID 50167 ENVELOPE ("Tue, 28 Jan 2014 15:08:59 +0100" "test" (("[email protected]" NIL "and" "127.0.0.1")) (("[email protected]" NIL "and" "127.0.0.1")) (("[email protected]" NIL "and" "127.0.0.1")) (("[email protected]" NIL "and" "127.0.0.1")) NIL NIL NIL "<[email protected]>") BODY[HEADER] {479} 
Return-Path: [email protected] 
Received: from [127.0.0.1] (localhost [127.0.0.1]) by A-PC ; Tue, 28 Jan 2014 15:08:59 +0100 
Message-ID: <[email protected]> 
Date: Tue, 28 Jan 2014 15:08:59 +0100 
From: "[email protected]" <[email protected]> 
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 
MIME-Version: 1.0 
To: [email protected] 
Subject: test 
Content-Type: text/plain; charset=UTF-8; format=flowed 
Content-Transfer-Encoding: 7bit 

BODY[TEXT]<0> {7} 
HELLO 
) 
A5 OK FETCH completed 
A6 FETCH 1 (FLAGS) 
* 1 FETCH (UID 50167 FLAGS (\Seen)) 
A6 OK FETCH completed 
A7 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE FLAGS BODYSTRUCTURE) 
* 1 FETCH (UID 50167 RFC822.SIZE 462 FLAGS (\Seen) INTERNALDATE "28-Jan-2014 15:08:59 +0100" ENVELOPE ("Tue, 28 Jan 2014 15:08:59 +0100" "test" (("[email protected]" NIL "and" "127.0.0.1")) (("[email protected]" NIL "and" "127.0.0.1")) (("[email protected]" NIL "and" "127.0.0.1")) (("[email protected]" NIL "and" "127.0.0.1")) NIL NIL NIL "<[email protected]>") BODYSTRUCTURE ("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL "test" "7bit" 7 2)) 
A7 OK FETCH completed 
A8 STORE 1 +FLAGS (\Seen) 
* 1 FETCH (FLAGS (\Seen) UID 50167) 
A8 OK STORE completed 
A9 CLOSE 
A9 OK CLOSE completed 
DEBUG: added an Authenticated connection -- size: 1 
IMAP DEBUG: IMAPProtocol noop 
A10 NOOP 
A10 OK NOOP completed 
DEBUG: connection available -- size: 1 
A11 SELECT INBOX 
* 1 EXISTS 
* 0 RECENT 
* FLAGS (\Deleted \Seen \Draft \Answered \Flagged) 
* OK [UIDVALIDITY 1378802425] current uidvalidity 
* OK [UIDNEXT 50168] next uid 
* OK [PERMANENTFLAGS (\Deleted \Seen \Draft \Answered \Flagged)] limited 
A11 OK [READ-WRITE] SELECT completed 
A12 SEARCH UNSEEN ALL 
* SEARCH 
A12 OK Search completed 
A13 CLOSE 
A13 OK CLOSE completed 
DEBUG: added an Authenticated connection -- size: 1 
IMAP DEBUG: IMAPProtocol noop 
A14 NOOP 
A14 OK NOOP completed 
A15 LOGOUT 
* BYE Have a nice day 
A15 OK Logout completed 
DEBUG: IMAPStore connection dead 
DEBUG: IMAPStore cleanup, force false 
DEBUG: IMAPStore cleanup done 

服務器hMailServer,在啓用SSL和NONSSL 127.0.0.1端口運行。

它的出現,我得到的郵件只有標題,但不是身體..

感謝您的任何建議,祝您有美好的一天!

+0

是否在SSL和非SSL端口上運行相同的服務器軟件? – Max

+0

如果打開JavaMail會話調試,協議跟蹤會顯示什麼內容?每種情況下您使用的是哪種類型的服務器? –

+0

我以郵編編輯的形式回覆 – Przunk

回答

0

我解決了這個問題。我遇到的錯誤鏈接到「無法加載BODYSTRUCTURE」錯誤(下面的鏈接 - 閱讀有關IMAP的複雜性)。

https://java.net/projects/javamail/pages/Exchange

http://www.oracle.com/technetwork/java/javamail/faq/index.html#imapserverbug

所有這一切必須做的,以獲得適當的身體信息,是創建郵件的副本,使用的MimeMessage構造函數。

// newMessages - it is array of messages from the mail inbox 
// create copy of the message using MimeMessage constructor 
MimeMessage message = new MimeMessage(newMessages[0]); // without it, the content was as in my first post 
// get content from the mssage 
Object messageContent = message.getContent(); 
// output it as a string 
System.out.println(messageContent.toString()); 

問題解決了,現在我收到了我想要的和我應該做的。唯一剩下的是,爲什麼SSL和NONSSL消息內容有區別?也許防病毒問題?

感謝您的幫助和關注。

祝您有美好的一天。