如何从MessageIDTerm获取Yahoo IMAP配置文件的消息?

时间:2013-09-19 09:52:15

标签: java javamail imap

我正在尝试从消息ID获取IMAP消息,这种情况对于Gmail的IMAP服务器是成功的,即 imap.gmail.com ,但我正在尝试使用Yahoo IMAP服务器 {{ 1}} imap.mail.yahoo.com返回null。

获取消息的代码块是 -

inbox.search(searArr[0])

我正在使用JavaMail-1.4.5版本。

这是一个 Yahoo 电子邮件的协议跟踪 -

Message[] msgs = null;
 MessageIDTerm[] searArr = new MessageIDTerm[]{new MessageIDTerm((String) strMessageID)};
 if (inbox != null && inbox.isOpen()) {
        msgs = inbox.search(searArr[0]);
  }
  for (int i = 0; i < msgs.length; i++) {
        System.out.println("Message: " + msgs[i]);
  }

类似我尝试使用一个 Gmail 邮件,其协议跟踪是 -

DEBUG: setDebug: JavaMail version ${mail.version}
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc.,${mail.version}]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: trying to connect to host "imap.mail.yahoo.com", port 993, isSSL true
* OK [CAPABILITY IMAP4rev1 ID NAMESPACE X-ID-ACLID UIDPLUS LITERAL+ CHILDREN XAPPLEPUSHSERVICE XYMHIGHESTMODSEQ AUTH=PLAIN AUTH=LOGIN AUTH=XYMCOOKIE AUTH=XYMECOOKIE AUTH=XYMCOOKIEB64 AUTH=XYMPKI] IMAP4rev1 imapgate-0.7.68_14.446672 imap404.mail.bf1.yahoo.com
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: LOGIN
DEBUG IMAPS: AUTH: XYMCOOKIE
DEBUG IMAPS: AUTH: XYMECOOKIE
DEBUG IMAPS: AUTH: XYMCOOKIEB64
DEBUG IMAPS: AUTH: XYMPKI
DEBUG IMAPS: protocolConnect login, host=imap.mail.yahoo.com, user=mymail@yahoo.com, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A0 OK AUTHENTICATE completed - Mailbox size in bytes is 180505
A1 CAPABILITY
* CAPABILITY IMAP4rev1 ID NAMESPACE X-ID-ACLID UIDPLUS LITERAL+ XAPPLEPUSHSERVICE XYMHIGHESTMODSEQ
A1 OK CAPABILITY completed
imaps://mymail%40yahoo.com@imap.mail.yahoo.com
DEBUG IMAPS: connection available -- size: 1
A2 SELECT nsharma
* 1 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1379585757] UIDs valid
* OK [UIDNEXT 13] Predicted next UID
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)] Permanent flags
* OK [HIGHESTMODSEQ 5925277392193028104]
A2 OK [READ-WRITE] SELECT completed; now in selected state
Press (U) to get only unread mails OR Press (A) to get all mails:
a
DEBUG IMAPS: IMAPProtocol noop
A3 NOOP
A3 OK NOOP completed
MAILS: 1
A4 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 1 FETCH (INTERNALDATE "19-Sep-2013 06:51:31 +0000" RFC822.SIZE 5747 ENVELOPE ("Thu, 19 Sep 2013 14:48:53 +0800 (SGT)" "test" (("Monu Jain" NIL "jainmonu_21" "yahoo.com")) (("Monu Jain" NIL "jainmonu_21" "yahoo.com")) (("Monu Jain" NIL "jainmonu_21" "yahoo.com")) (("mymail@yahoo.com" NIL "mymail" "yahoo.com")("mailmover1@yahoo.com" NIL "mailmover1" "yahoo.com")) NIL NIL NIL "<1379573333.18873.YahooMailNeo@web193105.mail.sg3.yahoo.com>"))
A4 OK FETCH completed
DATE: Thu Sep 19 12:18:53 IST 2013
FROM: Monu Jain <jainmonu_21@yahoo.com>
SUBJECT: test
A5 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "quoted-printable" 348 4 NIL NIL NIL NIL)("text" "html" ("charset" "utf-8") NIL NIL "quoted-printable" 646 8 NIL NIL NIL NIL) "alternative" ("boundary" "2118110249-1131840139-1379573333=:18873") NIL))
A5 OK FETCH completed
CONTENT: com.sun.mail.imap.IMAPInputStream@3b0a0018
Message Number: 1
Message ID: <1379573333.18873.YahooMailNeo@web193105.mail.sg3.yahoo.com>
A6 SEARCH HEADER Message-ID <1379573333.18873.YahooMailNeo@web193105.mail.sg3.yahoo.com> ALL
* SEARCH
A6 OK SEARCH completed

folder.search(MessageIDTerm)适用于Gmail。我发现两种类型的服务器在协议跟踪中返回的响应之间存在许多差异。有一件事我发现这两个消息的字符集是不同的(Gmail中的BODYSTRUCTURE中的CAPITAL响应) -

DEBUG: setDebug: JavaMail version ${mail.version}
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc.,${mail.version}]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 59.99.161.6 b10if5712468ibd.89
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
A0 OK Thats all she wrote! b10if5712468ibd.89
DEBUG IMAP: AUTH: XOAUTH
DEBUG IMAP: AUTH: XOAUTH2
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: PLAIN-CLIENTTOKEN
DEBUG: protocolConnect login, host=imap.gmail.com, user=mymail@gmail.com, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK mymail@gmail.com Ronak Jain authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH
A2 OK Success
imaps://mymail%40gmail.com@imap.gmail.com
DEBUG: connection available -- size: 1
A3 SELECT Inbox
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen XAOL-RECEIVED XAOL-BILLPAY-MAIL XAOL-READ $hasEmbedded $hasAttached XAOL-SENT XAOL-GOODCHECK-DONE XAOL-OFFICIAL-MAIL $NotJunk NotJunk XAOL-GOOD XAOL-CERTIFIED-MAIL $Junk)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen XAOL-RECEIVED XAOL-BILLPAY-MAIL XAOL-READ $hasEmbedded $hasAttached XAOL-SENT XAOL-GOODCHECK-DONE XAOL-OFFICIAL-MAIL $NotJunk NotJunk XAOL-GOOD XAOL-CERTIFIED-MAIL $Junk \*)] Flags permitted.
* OK [UIDVALIDITY 635312281] UIDs valid.
* 1102 EXISTS
* 0 RECENT
* OK [UIDNEXT 1532] Predicted next UID.
* OK [HIGHESTMODSEQ 199446]
A3 OK [READ-WRITE] Inbox selected. (Success)
Press (U) to get only unread mails OR Press (A) to get all mails:
a
DEBUG IMAP: IMAPProtocol noop
A4 NOOP
A4 OK Success
MAILS: 1102
A5 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 1 FETCH (RFC822.SIZE 57661 INTERNALDATE "02-Dec-2011 23:25:41 +0000" ENVELOPE ("Fri, 2 Dec 2011 17:35:54 -0500" "Hack Your Gadgets and Home to Teach Yourself a New Language" (("Lifehacker" NIL "email" "lifehacker.com")) (("<email=lifehacker.com@mail93.us1.rsgsv.net>" NIL "\"Lifehacker\"" NIL)) (("Lifehacker" NIL "email" "lifehacker.com")) ((NIL NIL "mymail" "gmail.com")) NIL NIL NIL "<251e609e1dba3888b86c21cc80f7e853471.20111202223539@mail93.us1.rsgsv.net>"))
A5 OK Success
DATE: Sat Dec 03 04:05:54 IST 2011
FROM: Lifehacker <email@lifehacker.com>
SUBJECT: Hack Your Gadgets and Home to Teach Yourself a New Language
A6 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "utf-8" "FORMAT" "fixed") NIL NIL "QUOTED-PRINTABLE" 21176 467 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "QUOTED-PRINTABLE" 33978 531 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "_----------=_MCPart_211467328") NIL NIL))
A6 OK Success
CONTENT: com.sun.mail.imap.IMAPInputStream@109fd086
Message Number: 1
Message ID: <251e609e1dba3888b86c21cc80f7e853471.20111202223539@mail93.us1.rsgsv.net>
DEBUG IMAP: IMAPProtocol noop
A7 NOOP
A7 OK Success
A8 SEARCH HEADER Message-ID <251e609e1dba3888b86c21cc80f7e853471.20111202223539@mail93.us1.rsgsv.net> ALL
* SEARCH 1
A8 OK SEARCH completed (Success)

雅虎是否不支持 MessageIDTerm 的搜索消息?在搜索SearchTerm for Yahoo时,只有我发现了有关Yahoo IMAP帐户的FlagTerm示例。

雅虎是否需要任何其他设置?或雅虎不支持搜索特定消息吗?

更新:

我已经尝试更新javaMail的方法 - 1.5

For Yahoo -- ("charset" "utf-8") 
For Gmail -- ("CHARSET" "utf-8" "FORMAT" "fixed")

我在用folder.search方法搜索时通过“utf-8”代替null它重新发送消息。改变是 -

  private int[] search(String msgSequence, SearchTerm term)
            throws ProtocolException, SearchException {

一条雅虎邮件的协议跟踪现在是在进行了上述更改后 -

return issueSearch(msgSequence, term, "utf-8");

这是获取消息的正确方法吗?

EDITED:覆盖SearchTerm类的方法match()以匹配messageID -

debug:
DEBUG: setDebug: JavaMail version ${mail.version}
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc.,${mail.version}]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: trying to connect to host "imap.mail.yahoo.com", port 993, isSSL true
* OK [CAPABILITY IMAP4rev1 ID NAMESPACE X-ID-ACLID UIDPLUS LITERAL+ CHILDREN XAPPLEPUSHSERVICE XYMHIGHESTMODSEQ AUTH=PLAIN AUTH=LOGIN AUTH=XYMCOOKIE AUTH=XYMECOOKIE AUTH=XYMCOOKIEB64 AUTH=XYMPKI] IMAP4rev1 imapgate-0.7.68_14.446672 imap427.mail.bf1.yahoo.com
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: LOGIN
DEBUG IMAPS: AUTH: XYMCOOKIE
DEBUG IMAPS: AUTH: XYMECOOKIE
DEBUG IMAPS: AUTH: XYMCOOKIEB64
DEBUG IMAPS: AUTH: XYMPKI
DEBUG IMAPS: protocolConnect login, host=imap.mail.yahoo.com, user=mymail@yahoo.com, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A0 OK AUTHENTICATE completed - Mailbox size in bytes is 139230
A1 CAPABILITY
* CAPABILITY IMAP4rev1 ID NAMESPACE X-ID-ACLID UIDPLUS LITERAL+ XAPPLEPUSHSERVICE XYMHIGHESTMODSEQ
A1 OK CAPABILITY completed
imaps://mymail%40yahoo.com@imap.mail.yahoo.com
DEBUG IMAPS: connection available -- size: 1
A2 SELECT Inbox
* 7 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1379569582] UIDs valid
* OK [UIDNEXT 12] Predicted next UID
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)] Permanent flags
* OK [HIGHESTMODSEQ 5926810751342850952]
A2 OK [READ-WRITE] SELECT completed; now in selected state
DEBUG IMAPS: IMAPProtocol noop
A3 NOOP
A3 OK NOOP completed
MAILS: 7
A4 FETCH 1 (ENVELOPE INTERNALDATE RFC822.SIZE)
* 1 FETCH (INTERNALDATE "19-Sep-2013 05:57:28 +0000" RFC822.SIZE 9404 ENVELOPE ("Thu, 19 Sep 2013 11:27:07 +0530" "Fwd: Commander Report on 2013-09-13T13:38:14" (("Neelam Sharma" NIL "neelamudr" "gmail.com")) (("Neelam Sharma" NIL "neelamudr" "gmail.com")) (("Neelam Sharma" NIL "neelamudr" "gmail.com")) ((NIL NIL "mymail" "yahoo.com")(NIL NIL "mailmover1" "yahoo.com")) NIL NIL "<2013042856.0.1379059694398.JavaMail.admin@machine-166>" "<CAJDwy1HgUQzmRYmMZRY-r9-2wCQGDcpvwR+4Chn82L+kZV1p0A@mail.gmail.com>"))
A4 OK FETCH completed
DATE: Thu Sep 19 11:27:07 IST 2013
A5 FETCH 1 (BODY.PEEK[HEADER.FIELDS (Message-Id)])
* 1 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {84}
Message-ID: <CAJDwy1HgUQzmRYmMZRY-r9-2wCQGDcpvwR+4Chn82L+kZV1p0A@mail.gmail.com>

)
A5 OK FETCH completed
Headers: [Ljava.lang.String;@13e1329e
FROM: Neelam Sharma <neelamudr@gmail.com>
SUBJECT: Fwd: Commander Report on 2013-09-13T13:38:14
A6 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "iso-8859-1") NIL NIL "7bit" 2288 52 NIL NIL NIL NIL)("text" "html" ("charset" "iso-8859-1") NIL NIL "quoted-printable" 3408 55 NIL NIL NIL NIL) "alternative" ("boundary" "001a1130ca2e86248804e6b63a47") NIL))
A6 OK FETCH completed
CONTENT: com.sun.mail.imap.IMAPInputStream@fd49559
Message Number: 1
Message ID: <CAJDwy1HgUQzmRYmMZRY-r9-2wCQGDcpvwR+4Chn82L+kZV1p0A@mail.gmail.com>
A7 SEARCH CHARSET utf-8 HEADER Message-ID <CAJDwy1HgUQzmRYmMZRY-r9-2wCQGDcpvwR+4Chn82L+kZV1p0A@mail.gmail.com> ALL
A7 NO [BADCHARSET] SEARCH SEARCH error: charset not supported
DEBUG IMAPS: IMAPProtocol noop
A8 NOOP
A8 OK NOOP completed
A9 SEARCH CHARSET utf-8 HEADER Message-ID <CAJDwy1HgUQzmRYmMZRY-r9-2wCQGDcpvwR+4Chn82L+kZV1p0A@mail.gmail.com> 1:7
A9 NO [BADCHARSET] SEARCH SEARCH error: charset not supported
A10 FETCH 2 (BODY.PEEK[HEADER.FIELDS (Message-ID)])
* 2 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {84}
Message-ID: <CAJDwy1EG+w_toT+L1yRW3+FxdFULUq_7o-7jX_NdW9H8PCDuFA@mail.gmail.com>

)
A10 OK FETCH completed
A11 FETCH 3 (BODY.PEEK[HEADER.FIELDS (Message-ID)])
* 3 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {84}
Message-ID: <CAJDwy1EbOgtCQj+PzrYzifFZwbOvShV6LTzZ+CJBRGm+xZhvvg@mail.gmail.com>

)
A11 OK FETCH completed
A12 FETCH 4 (BODY.PEEK[HEADER.FIELDS (Message-ID)])
* 4 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {84}
Message-ID: <CAJDwy1Fr_HRMAmJKBoUEC+UfkArSvDPFrL8UZCyM8rMkt7uGGQ@mail.gmail.com>

)
A12 OK FETCH completed
A13 FETCH 5 (BODY.PEEK[HEADER.FIELDS (Message-ID)])
* 5 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {76}
Message-ID: <1379573294.59617.YahooMailNeo@web193103.mail.sg3.yahoo.com>

)
A13 OK FETCH completed
A14 FETCH 6 (BODY.PEEK[HEADER.FIELDS (Message-ID)])
* 6 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {76}
Message-ID: <1379573283.17330.YahooMailNeo@web193105.mail.sg3.yahoo.com>

)
A14 OK FETCH completed
A15 FETCH 7 (BODY.PEEK[HEADER.FIELDS (Message-ID)])
* 7 FETCH (BODY[HEADER.FIELDS (MESSAGE-ID)] {76}
Message-ID: <1379573333.18873.YahooMailNeo@web193105.mail.sg3.yahoo.com>

)
A15 OK FETCH completed
Found message #0: Fwd: Commander Report on 2013-09-13T13:38:14
Message: com.sun.mail.imap.IMAPMessage@3b733273
FROM12: Neelam Sharma <neelamudr@gmail.com>
SUBJECT12: Fwd: Commander Report on 2013-09-13T13:38:14
CONTENT12: com.sun.mail.imap.IMAPInputStream@6e912008
Message Number12: 1
Message ID12: <CAJDwy1HgUQzmRYmMZRY-r9-2wCQGDcpvwR+4Chn82L+kZV1p0A@mail.gmail.com>
******************************************

此搜索返回message-ID给出的消息。

由于

Neelam Sharma

2 个答案:

答案 0 :(得分:1)

我的问题通过覆盖SearchTerm类的方法match()以匹配messageID来解决 -

  SearchTerm messageIDTerm= new SearchTerm() {
     @Override
     public boolean match(Message message) {
     try {
          if (((MimeMessage) message).getMessageID().contains(sMessageId)) {
                   return true;
          }
         } catch (MessagingException ex) {
                    ex.printStackTrace();
         }
          return false;
      }
 };
msgs = folder.search(messageIDTerm);

此搜索返回message-ID给出的消息。我使用的是JavaMail-1.5版本。它有效!!

答案 1 :(得分:0)

我假设您已确保邮箱中确实存在具有该Message-ID的邮件?

看看Yahoo IMAP服务器实际返回的内容会很有趣。尝试打开Session debugging并查看协议跟踪。 Yahoo IMAP服务器可能不支持此功能;它似乎只支持现有的移动客户端需要。