使用messageid进行Imap搜索

时间:2016-05-30 07:42:56

标签: java email outlook javamail

我有一个电子邮件处理器java应用程序,它连接outlook并列出文件夹消息,然后决定它是否下载电子邮件。我的一些电子邮件有以下问题。

列出以下代码:

Session session = Session.getInstance(props, new myAuthenticator());
store = session.getStore(PROTOCOL);
store.connect(HOST, USERNAME, PASSWORD);
inbox_unknown = store.getDefaultFolder().getFolder("INBOX_UNKNOWN");
inbox_unknown.open(Folder.READ_ONLY);

messages = inbox_unknown.getMessages();
for(Message m : messages){
  Enumeration headers = m.getAllHeaders();
  while(headers.hasMoreElements()){
    Header element = (Header)headers.nextElement();
    if ("Message-ID".equals(element.getName()))
      System.err.println(element.getValue());
  }
}

结果是:

<CAFSBYLHvG9d=MkE2Gf+q_EgoaAN8yRVHHsg5emO0w92GV6-7LA@mail.gmail.com>
<CAFSBYLGs-w4=JrgrYh_6kpiEc9NhjCsc76gXVvLWyW-=QN+kMw@mail.gmail.com>

但是当我使用以下代码搜索这些消息ID时:

    messages = inbox_unknown.search(new MessageIDTerm("<CAFSBYLGs-w4=JrgrYh_6kpiEc9NhjCsc76gXVvLWyW-=QN+kMw@mail.gmail.com>"));//not found
    messages = inbox_unknown.search(new MessageIDTerm("<CAFSBYLHvG9d=MkE2Gf+q_EgoaAN8yRVHHsg5emO0w92GV6-7LA@mail.gmail.com>"));//found

虽然列出了标识为<CAFSBYLHvG9d=MkE2Gf+q_EgoaAN8yRVHHsg5emO0w92GV6-7LA@mail.gmail.com>的邮件,但当我搜索此单个项目时,我会得到空的返回数组。对于其他消息ID,它按预期返回消息对象。你知道这可能是什么问题吗? PS:这封邮件来自同一个人,这只是例如,虽然可以列出,但仍有许多类似的邮件未找到。

注意:使用java7,java.mail-1.4 api,ms outlook服务器,IMAP4作为协议。 注2:添加了java调试输出

DEBUG: setDebug: JavaMail version 1.5.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "outlook.abck.com.tr", port 143, isSSL false
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=outlook.abck.com.tr, user=usera, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 OK AUTHENTICATE completed.
A2 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A2 OK CAPABILITY completed.
DEBUG IMAP: AUTH: NTLM
DEBUG IMAP: AUTH: GSSAPI
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: connection available -- size: 1
A3 EXAMINE Test
* 2 EXISTS
* 2 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS ()] Permanent flags
* OK [UIDVALIDITY 268114] UIDVALIDITY value
* OK [UIDNEXT 4] The next unique identifier value
A3 OK [READ-ONLY] EXAMINE completed.
A4 SEARCH HEADER Message-ID <CAFSBYLGs-w4=JrgrYh_6kpiEc9NhjCsc76gXVvLWyW-=QN+kMw@mail.gmail.com> ALL
* SEARCH
A4 OK SEARCH completed.
A5 SEARCH HEADER Message-ID <CAFSBYLHvG9d=MkE2Gf+q_EgoaAN8yRVHHsg5emO0w92GV6-7LA@mail.gmail.com> ALL
* SEARCH 2
A5 OK SEARCH completed.
A6 FETCH 1 (BODY.PEEK[HEADER])
* 1 FETCH (BODY[HEADER] {1403}
MIME-Version: 1.0
Received: from mail01.abck.com.tr (62.108.64.49) by
 EXCPRDHCS02.abck.abc (10.81.42.19) with Microsoft SMTP Server
 (TLS) id 14.3.195.1; Tue, 17 May 2016 14:31:39 +0300
Received: from mail-yw0-f170.google.com ([209.85.161.170])  by
 mail01.abck.com.tr with ESMTP/TLS/AES128-GCM-SHA256; 17 May 2016
 14:31:31 +0300
Received: by mail-yw0-f170.google.com with SMTP id x189so11988152ywe.3;
        Tue, 17 May 2016 04:31:31 -0700 (PDT)
Received: by 10.37.14.212 with HTTP; Tue, 17 May 2016 04:31:30 -0700 (PDT)
Subject: =?utf-8?B?VEFMxLBNQVQ=?=
Thread-Topic: =?utf-8?B?VEFMxLBNQVQ=?=
Thread-Index: AQHRsC+uNbCd970/KU2ZmbAy2JPC2w==
Date: Tue, 17 May 2016 14:31:30 +0300
Message-ID:
 <CAFSBYLGs-w4=JrgrYh_6kpiEc9NhjCsc76gXVvLWyW-=QN+kMw@mail.gmail.com>
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Anonymous
X-MS-Exchange-Organization-AuthSource: EXCPRDHCS02.abck.abc
X-MS-Has-Attach: yes
X-MS-Exchange-Organization-SCL: 0
X-MS-TNEF-Correlator:
Content-Type: multipart/mixed;
    boundary="_003_CAFSBYLGsw4JrgrYh6kpiEc9NhjCsc76gXVvLWyWQNkMwmailgmailc_"

)
A6 OK FETCH completed.
A7 FETCH 2 (BODY.PEEK[HEADER])
* 2 FETCH (BODY[HEADER] {4900}
MIME-Version: 1.0
Received: from mail01.abck.com.tr (62.108.64.49) by
 EXCPRDHCS01.abck.abc (10.81.42.18) with Microsoft SMTP Server
 (TLS) id 14.3.279.2; Wed, 25 May 2016 10:30:08 +0300
IronPort-PHdr: =?us-ascii?q?9a23=3AVhJs6B+zz3F7Iv9uRHKM819IXTAuvvDOBiVQ1KB7?=
 =?us-ascii?q?0eIcTK2v8tzYMVDF4r011RmSDdSdt6gP0rCN+4nbGkU+or+5+EgYd5JNUxJXwe?=
 =?us-ascii?q?43pCcHRPC/NEvgMfTxZDY7FskRHHVs/nW8LFQHUJ2mPw6anHS+4HYoFwnlMkIt?=
 =?us-ascii?q?f6KuSt+U0578jrrps7ToICx2xxOFKYtoKxu3qQiD/uI3uqBFbpgL9x3Sv3FTcP?=
 =?us-ascii?q?5Xz247bXianhL7+9vitMU7q3cYk7sb+sVBSaT3ebgjBfwdVWx+cjN92Mq+/zTH?=
 =?us-ascii?q?TAXH3nYcVmQImxwAQy3I4xiydZr3ribg/tFg0iSBPIjVSro1ER6r6aRiQx7siS?=
 =?us-ascii?q?gGdnRxpGzTi8I2lK9bpBO/rhpXzY/SaYeOPvN4feXWetZcWXAXGo4beiBISqqg?=
 =?us-ascii?q?ZpBHT9EcN+sQl4DvrkEKpB63TSmtFubi1ndti2X10L87kq5pRQzK1UopA9sVmH?=
 =?us-ascii?q?rVqtzzL60bV+Pzx67NiynZObcexT756Y3BbjgvoPeFW6l6eMzSj0IoEkXZlAa+?=
 =?us-ascii?q?s4vgag248sVFnEW9QKI0Tv+jhmAqsQRsqyKHycIli42PjYUQnAOXvR5lyZo4cI?=
 =?us-ascii?q?XrAHVwZsSpRd4J73mX?=
X-Cloudmark-SP-Filtered: true
X-Cloudmark-SP-Result: =?us-ascii?q?v=3D2=2E1_cv=3DF5f3DvVN_c=3D1_sm=3D1_tr?=
 =?us-ascii?q?=3D0_a=3Dyrkiwgmsf1kA=3A10_a=3DxWK-KtZqUfIA=3A10?=
 =?us-ascii?q?_a=3De186Yzns-9f1=5FKZu-cIA=3A9_a=3DQEXdDO2ut3YA=3A10_a=3DOKp-?=
 =?us-ascii?q?Gz0vqPG3m8aTRrIA=3A9?=
 =?us-ascii?q?_a=3DKQqxNPgzF0kA=3A10?=
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0HiAwB/U0VXf7OhVdFbhQoGpn6HY4NNg?=
 =?us-ascii?q?xSCJYQTBCRxB4YkBzwQAQEBAQEBAQECDwEBCQsLCSEbFIIuCjkQVQIrTA0RHQE?=
 =?us-ascii?q?bHgMSAwUBAgUSAQELGAIEFQEKAREBBQEREQEYHIdyAQMXlVGPQoExPjGKBoE1g?=
 =?us-ascii?q?WqCWAWIKwoZJw1Sg1cMFwYCBhCSI4JZBY5VhwWCXYMrgWmJDIFTAY1Ijg0SHoE?=
 =?us-ascii?q?PN4QbOTKKBwEBAQ?=
X-IPAS-Result: =?us-ascii?q?A0HiAwB/U0VXf7OhVdFbhQoGpn6HY4NNgxSCJYQTBCRxB4Y?=
 =?us-ascii?q?kBzwQAQEBAQEBAQECDwEBCQsLCSEbFIIuCjkQVQIrTA0RHQEbHgMSAwUBAgUSA?=
 =?us-ascii?q?QELGAIEFQEKAREBBQEREQEYHIdyAQMXlVGPQoExPjGKBoE1gWqCWAWIKwoZJw1?=
 =?us-ascii?q?Sg1cMFwYCBhCSI4JZBY5VhwWCXYMrgWmJDIFTAY1Ijg0SHoEPN4QbOTKKBwEBA?=
 =?us-ascii?q?Q?=
Received: from mail-yw0-f179.google.com ([209.85.161.179])  by
 mail01.abck.com.tr with ESMTP/TLS/AES128-GCM-SHA256; 25 May 2016
 10:29:57 +0300
Received: by mail-yw0-f179.google.com with SMTP id o16so39498708ywd.2;
        Wed, 25 May 2016 00:29:57 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:date:message-id:subject:from:to;
        bh=w8xSZyobRcFMKx+IrtarhnHXAm9n7xJEYfaDMmptcKI=;
        b=WN8u7uLAyNhcAoB+oQ5xbsKdpV4z4BGWD6CrryDyJqq7tosj82QH3d3MQWHaqf2mBa
         AqlmCBq5w3ntokCoKrQlzdQNGecJ7AvIrc/CPFkYLeT3MK1kcY8gzDANJC/Pm5NO75td
         S/mBW9goEL1gKFwpd/DahOzIwKbT5UEz9hthcTU5JIPwJR305/boxMIP3zcu1M+WPVPY
         RhnojBbe41iCfqemB11IIvFeyRMqck6x7dDnWnejriEP2Gp8x4s2FYLz5rysEloNe0wU
         qKmmNXtsIStwJhdk8jjByMW/UsK+94t3+jr+rzbTyYndh/Hs3SbZsmDQipBG8+SHxRQu
         e/TQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:date:message-id:subject:from:to;
        bh=w8xSZyobRcFMKx+IrtarhnHXAm9n7xJEYfaDMmptcKI=;
        b=iXyugXgd1ER8sfK9PJXcq88MO8z2/VKl31XNf/Bp2E1BvC42ZxLsAGjeHKL6KqqzFb
         CNdHhBLnyfAnGoaa9pIK+qoC2HT69e323Rmym3w6HxQr1ot396xThXXxL22ZManrpICu
         RAPegTlsmXQ1VBqhYQjgX8NwX6FtoqtzBW9tSOnl+HTfdyv3CNNf0mAysA7jyCHwQy6j
         Nc+JK/uUZjccYkic672MiDSUa4FfwJSx0L3nyWI/CZMVDaNVW3mtp04cpgvtshLkloLn
         61bz3dlGNL1g3gIVwNFiY8SLGxFdPzRc7xJ9VJEgXoA1h2OO2QVqyEUUyivhzehbgvL5
         Bhtw==
X-Gm-Message-State: ALyK8tLPd9mwwszKuBwk9ndltkf3tyZ91virPLkoGP8PigQLffqE7vB7NVGiauqhPC9vcXsZnsU/mzB4zSTZbg==
X-Received: by 10.13.217.151 with SMTP id b145mr1363469ywe.53.1464161395972;
 Wed, 25 May 2016 00:29:55 -0700 (PDT)
Received: by 10.37.14.212 with HTTP; Wed, 25 May 2016 00:29:55 -0700 (PDT)
Date: Wed, 25 May 2016 10:29:55 +0300
Message-ID: <CAFSBYLHvG9d=MkE2Gf+q_EgoaAN8yRVHHsg5emO0w92GV6-7LA@mail.gmail.com>
Subject: =?UTF-8?B?QsOcxZ5SQSBFVMSwIE1VU1RBRkEgRVTEsCBLSURFTSBUQVpNxLBOQVRJIMOWREVNRSBUQQ==?=
    =?UTF-8?B?TMSwTUFUSQ==?=
Content-Type: multipart/mixed; boundary="001a114fa43ee6ce4a0533a5a3c5"
Return-Path: abc@gmail.com
X-MS-Exchange-Organization-AuthSource: EXCPRDHCS01.abck.abc
X-MS-Exchange-Organization-AuthAs: Anonymous
X-MS-Exchange-Organization-AVStamp-Mailbox: SMEXw]nP;1254500;0;This mail has
 been scanned by Trend Micro ScanMail for Microsoft Exchange;
X-MS-Exchange-Organization-SCL: 0
X-PP-Proceessed: 162a0e36-1ac8-484f-b83f-d5c2dc8a3c0b

)
A7 OK FETCH completed.
A8 CLOSE
A8 OK CLOSE completed.
DEBUG IMAP: added an Authenticated connection -- size: 1
A9 LOGOUT
* BYE Microsoft Exchange Server 2010 IMAP4 server signing off.
A9 OK LOGOUT completed.
DEBUG IMAP: IMAPStore connection dead
DEBUG IMAP: IMAPStore cleanup, force false
DEBUG IMAP: IMAPStore cleanup done

0 个答案:

没有答案