如何从 imap 只下载新邮件?

发布于 2024-10-12 10:59:32 字数 274 浏览 7 评论 0原文

我有一个用于使用 imap 存档电子邮件的应用程序。此应用程序中还有许多需要存档的 imap 帐户。

此时,应用程序会不时连接到 imap 帐户并仅下载新电子邮件。我的问题是,每次连接到 imap 帐户时,它都会验证所有文件夹中的所有电子邮件,并仅下载尚未下载的电子邮件(我存储所有电子邮件的消息 ID,并仅下载消息 ID 为不存储)。 所以我想知道是否有替代方案,因为验证所有电子邮件需要一些时间(对于 10-20K,需要 2-5 分钟)。

我使用 JavaMail API 连接到 imap 帐户。

I have an application that is used to archive emails using imap. Also in this application are many imap accounts that need to be archived.

In this moment from time to time the application connects to imap accounts and download only new emails. My issue is that every time when it connects to an imap account it verifies all emails from all folders and downloads only emails that aren't downloaded yet (I store Message-ID for all emails and download only emails that have an Message-ID that is not stored).
So I want to know if there is an alternative for this, because it takes some time to verify all emails (for 10-20K it takes 2-5 minutes).

I use JavaMail API to connect to imap accounts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫七 2024-10-19 10:59:32

javadoc 帮助:

IMAPFolder 提供方法:

getMessagesByUID(long start, long end) 和

getUID(Message message)

使用 getUID(),您可以获得已下载的最后一条消息的 UID。使用 getMessagesByUID,您可以将下载的最后一条消息定义为起始范围,并使用 getUIDNext() 方法查找作为范围末尾的最后一条消息。

The javadoc helps:

IMAPFolder provides the methods:

getMessagesByUID(long start, long end) and

getUID(Message message)

With getUID() you can get the UID of the last message you already have downloaded. With getMessagesByUID you can define this last message you have downloaded as start-range and look with the method getUIDNext() to find the last message which would be the end of the range.

零度° 2024-10-19 10:59:32

仅检查标题,当您到达已知的(最后一个已知的)时,退出:

例如(我今天感觉特别好),这是真实生产代码的例外(某些部分被删除,因此它可能无法编译,状态。处理的是一些集合,最好是 LinkedHashMap 代理 [keySet()] (和一些最大边界布尔值 removeEldestEntry())

 try {
      store = mailSession.getStore("imap");
      try {
        store.connect();
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        int count = folder.getMessageCount();
        for(int localProc=0, chunk=49;localProc<10 && count>0; count -=chunk+1){


          Message messages[] = folder.getMessages(Math.max(count-chunk, 1), count);

          FetchProfile fp = new FetchProfile();
          fp.add(FetchProfile.Item.ENVELOPE);
          fp.add("Message-ID");
//add more headers, if need be
          folder.fetch(messages,fp);

          for (int i=messages.length;--i>=0;) {

            //can check abort request here
            Message  message = messages[i];


            String msgId = getHeader(message,"Message-ID");
            if (msgId!=null && !state.processed.add(msgId)){            
              if (++localProc>=10){
                break;
              }
              continue;
            }
///process here, catch exception, etc..
          }
        }

        folder.close(false);        
      } catch (MessagingException e) {
        logger.log(Level.SEVERE, "Mail messaging exception", e);
      }
    } catch (NoSuchProviderException e) {
      logger.log(Level.SEVERE, "No mail provider", e);
    }

    if(store != null) {
      try {
        store.close();
      } catch (MessagingException e) {}
    }

check only the headers and when you reach a known (the last known), bail out:

for instance (i feel extra nice today) and that's an except from real production code (some parts were cut, so it might not compile, state.processed is some set preferrably LinkedHashMap surrogate [keySet()] (and w/ some max boundary boolean removeEldestEntry())

 try {
      store = mailSession.getStore("imap");
      try {
        store.connect();
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        int count = folder.getMessageCount();
        for(int localProc=0, chunk=49;localProc<10 && count>0; count -=chunk+1){


          Message messages[] = folder.getMessages(Math.max(count-chunk, 1), count);

          FetchProfile fp = new FetchProfile();
          fp.add(FetchProfile.Item.ENVELOPE);
          fp.add("Message-ID");
//add more headers, if need be
          folder.fetch(messages,fp);

          for (int i=messages.length;--i>=0;) {

            //can check abort request here
            Message  message = messages[i];


            String msgId = getHeader(message,"Message-ID");
            if (msgId!=null && !state.processed.add(msgId)){            
              if (++localProc>=10){
                break;
              }
              continue;
            }
///process here, catch exception, etc..
          }
        }

        folder.close(false);        
      } catch (MessagingException e) {
        logger.log(Level.SEVERE, "Mail messaging exception", e);
      }
    } catch (NoSuchProviderException e) {
      logger.log(Level.SEVERE, "No mail provider", e);
    }

    if(store != null) {
      try {
        store.close();
      } catch (MessagingException e) {}
    }
别靠近我心 2024-10-19 10:59:32

根据 SEEN 标志进行过滤。该标志用于查找新消息。需要注意的是,如果您的用户使用多个阅读器,那么可能会看到它使用另一个阅读器。

Filter on the SEEN flag. This flag is intended for finding new messages. The one Caveat is that if your user is using multiple readers, then it may have been seen using another reader.

甜扑 2024-10-19 10:59:32

作为标头一部分的 message-Id 始终是唯一的,即使您手动设置它也是如此。我已经使用 gamil 和racksoace 对其进行了测试。

message-Id which comes as part of the header is always unique even if u set it manually .i have tested it with gamil and racksoace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文