JavaMail IMAP消息内容SSL / NON SSL

时间:2014-01-22 13:31:59

标签: java ssl javamail imap

我遇到了JavaMail中IMAP的奇怪问题。

我已经在JavaMail方面有很多了解,我也知道在哪里寻找问题/答案,但我找不到解决这个问题的方法。

我有IMAP频道,我可以在其上启用或禁用SSL。 当我创建新的电子邮件消息(让我们说,主题:“TEST”,正文:“HELLO”)并从Thunderbird客户端发送它,以便能够使用我的IMAP频道下载它时,事情变得很奇怪。

在SSL ENABLED上,一切正常。 (在我的IMAP频道中)我从电子邮件文件夹中获取消息,在成功连接到商店之后,然后我从此消息获取getContent()(它始终是字符串类型,而不是MimeMultipart或Multipart),输出是“HELLO”。

然而,在SSL DISABLED上,一切都运行良好,但我收到不同的身体。它看起来像整个消息解析(见下文)

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

HELLO

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

Return-Path: and@127.0.0.1
Received: from [127.0.0.1] (localhost [127.0.0.1]) by PC ; Wed, 22 Jan 2014 13:23:17 +0100
Message-ID: <52DFB835.9020300@127.0.0.1>
Date: Wed, 22 Jan 2014 13:23:17 +0100
From: "and@127.0.0.1" <and@127.0.0.1>
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6
MIME-Version: 1.0
To: and@127.0.0.1
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,FLAGS,ENVELOPE}

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, user=and@127.0.0.1, 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, user=and@127.0.0.1, password=<non-null>
A1 LOGIN and@127.0.0.1 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" (("and@127.0.0.1" NIL "and" "127.0.0.1")) (("and@127.0.0.1" NIL "and" "127.0.0.1")) (("and@127.0.0.1" NIL "and" "127.0.0.1")) (("and@127.0.0.1" NIL "and" "127.0.0.1")) NIL NIL NIL "<52E7B9FB.2030409@127.0.0.1>") BODY[HEADER] {479}
Return-Path: and@127.0.0.1
Received: from [127.0.0.1] (localhost [127.0.0.1]) by A-PC ; Tue, 28 Jan 2014 15:08:59 +0100
Message-ID: <52E7B9FB.2030409@127.0.0.1>
Date: Tue, 28 Jan 2014 15:08:59 +0100
From: "and@127.0.0.1" <and@127.0.0.1>
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6
MIME-Version: 1.0
To: and@127.0.0.1
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" (("and@127.0.0.1" NIL "and" "127.0.0.1")) (("and@127.0.0.1" NIL "and" "127.0.0.1")) (("and@127.0.0.1" NIL "and" "127.0.0.1")) (("and@127.0.0.1" NIL "and" "127.0.0.1")) NIL NIL NIL "<52E7B9FB.2030409@127.0.0.1>") 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,在127.0.0.1上运行,启用了SSL并启用了NONSSL端口。

看来,我只收到来自邮件的标题,但不是正文..

感谢您的任何建议,祝您有个美好的一天!

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。我遇到的错误链接到“无法加载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消息内容之间存在差异?也许杀毒问题?

感谢您的帮助和兴趣。

度过美好的一天。