简体   繁体   English

JavaMail IMAP消息内容SSL / NON SSL

[英]JavaMail IMAP message content SSL/NON SSL

I am experiencing weird problems over IMAP in JavaMail. 我在JavaMail中的IMAP上遇到奇怪的问题。

I already know alot in terms of JavaMail and I also know where to look for Questions/Answers, yet I cannot find a solution for this issue. 我已经很了解JavaMail,也知道在哪里查找问题/答案,但是我找不到该问题的解决方案。

I have IMAP channel, on which I can set SSL enabled or disabled. 我有IMAP通道,可以在其上设置启用或禁用SSL。 When I create new email message (let's say, Subject: "TEST", Body: "HELLO") and send it from Thunderbird client, to be able to download it with my IMAP channel, things are getting weird. 当我创建新的电子邮件消息(例如,主题:“ TEST”,正文:“ HELLO”)并从Thunderbird客户端发送该消息,以便能够通过我的IMAP频道下载该消息时,事情变得很奇怪。

On SSL ENABLED, everything works fine. 在启用SSL的情况下,一切正常。 (in my IMAP channel) I get message from email folder, after successfuly connecting to the store, then i getContent() from this message (it is ALWAYS String type, not MimeMultipart or Multipart), and output is "HELLO". (在我的IMAP通道中)在成功连接到商店后,我从电子邮件文件夹中获取了消息,然后从该消息中获取了getContent()(始终为字符串类型,而不是MimeMultipart或Multipart),并且输出为“ HELLO”。

However, on SSL DISABLED, everything also works fine, but I receive different body. 但是,在SSL DISABLED上,一切也都可以正常工作,但是我收到的是不同的正文。 It looks like whole message parsed (look below) 看起来整个邮件都已解析(请看下面)

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

HELLO

message.getContent().toString(); message.getContent()的toString(); NOT using SSL 不使用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

Does anyone have any idea what's going on? 有人知道发生了什么吗? Below are fragments of my code (to show you most important parts: 以下是我的代码片段(向您展示最重要的部分:

// 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!)
}

// edit : added debug info // edit 2 : debug info contains now full details with fetching folder, using { CONTENT_INFO, FLAGS, ENVELOPE } //编辑:添加了调试信息//编辑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

server is hMailServer, running on 127.0.0.1 with SSL and NONSSL ports enabled. 服务器是hMailServer,在启用SSL和NONSSL端口的情况下在127.0.0.1上运行。

It appears, that I get only headers from mail, but not the body.. 看来,我仅从邮件中获得标头,但未从正文中获得。

Thanks for any suggestions, have a great day! 感谢您的任何建议,祝您愉快!

I fixed the problem. 我解决了这个问题。 The bug I have encountered was linked to the "Unable to load BODYSTRUCTURE" error (links below - read about IMAP complexity). 我遇到的错误与“无法加载BODYSTRUCTURE”错误有关(下面的链接-了解IMAP复杂性)。

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

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

All that had to be done, to get proper body message, was creating copy of the message, using MimeMessage constructor. 为了获得正确的正文消息,所有要做的就是使用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());

The problem is solved, now I receive what I want and what I should. 问题已经解决,现在我收到了我想要的东西和应该得到的东西。 The only thing left is, why there was a difference between SSL and NONSSL message content? 剩下的唯一一件事是,为什么SSL和NONSSL消息内容之间有区别? Maybe antivirus problem? 也许是防病毒问题?

Thanks for help and interest. 感谢您的帮助和关注。

Have a great day. 祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM