How to get message from MessageIDTerm for Yahoo IM

2019-07-17 08:49发布

问题:

I am trying to get IMAP message from message ID, this case is successful for IMAP server for Gmail i.e imap.gmail.com but same I am trying with Yahoo IMAP server imap.mail.yahoo.com, inbox.search(searArr[0]) is returning null.

My code block to get message is -

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]);
  }

I am using JavaMail-1.4.5 version.

This is protocol trace of one Yahoo email -

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

Similar I try with one Gmail Mail, its protocol trace is -

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)

folder.search(MessageIDTerm) working fine for Gmail. I found there are many differences between response returned in protocol trace by both types of server. One thing I found that the charset of both messages are different (CAPITAL response in BODYSTRUCTURE in Gmail) -

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

Does Yahoo not support search message by MessageIDTerm?? On searching about SearchTerm for Yahoo, only I found the sample about FlagTerm in case of Yahoo IMAP account.

Does Yahoo require any other settings? OR Does Yahoo not support search for particular message ??

UPDATE :

I have try updating method of javaMail - 1.5

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

I passed "utf-8" in place of null it retruning message while seraching with folder.search method. The change is -

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

The protocol trace of one Yahoo mail is now after making above changes -

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>
******************************************

Is it a proper way to getting message ??

EDITED : Overwritten the method match() of SearchTerm class as to 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);

This search returns message given by message-ID.

Thanks

Neelam Sharma

回答1:

My issue resolved by overwriting the method match() of SearchTerm class as to 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);

This search returns message given by message-ID. I am using JavaMail-1.5 version. It works!!



回答2:

I assume you've made sure that a message with that Message-ID actually does exist in the mailbox?

It would be interesting to see what the Yahoo IMAP server is actually returning. Try turning on Session debugging and look at the protocol trace. It's possible the Yahoo IMAP server doesn't support this; it seems to only support what existing mobile clients need.