IMAP - 获取上一个和下一个 UID

发布于 2024-09-27 00:33:52 字数 72 浏览 6 评论 0原文

使用标准规范 IMAP 命令,如何根据传递的 UID 确定邮箱中的上一个和下一个 UID,并按日期对邮箱消息进行排序,最新的在前?

Using standard spec IMAP commands, how can I determine the previous and next UIDs in a mailbox based on a passed UID, sorting the mailbox messages by date, newest first?

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

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

发布评论

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

评论(5

老娘不死你永远是小三 2024-10-04 00:33:52

我找到了带有以下代码的新邮件 UID

   @Override
        public void messagesAdded(MessageCountEvent e) {
         try {
             long newMailUID = ((IMAPFolder) e.getSource()).getUIDNext()
         } catch (MessagingException e1) {
             e1.printStackTrace();
         }
       }

I have found new mail UID with below code.

   @Override
        public void messagesAdded(MessageCountEvent e) {
         try {
             long newMailUID = ((IMAPFolder) e.getSource()).getUIDNext()
         } catch (MessagingException e1) {
             e1.printStackTrace();
         }
       }
趁年轻赶紧闹 2024-10-04 00:33:52

假设“按日期对邮箱邮件进行排序,最新的在前”是指邮件添加到邮箱的时间,而不是INTERNALDATE日期 header:

A001 SEARCH UID 82342
* SEARCH 83
A001 OK SEARCH completed

A002 FETCH 82,84 UID
* FETCH 82 (UID 82309)
* FETCH 84 (UID 82343)
A002 OK FETCH completed

需要注意的事项:

  • 如果邮箱中不存在 UID,SEARCH 将不会返回任何结果。
  • 如果 UID 与邮箱中的第一条/最后一条消息匹配,请勿尝试获取之前/之后的消息,否则您将收到 BAD 响应。

如果您不想使用 SEARCH,也可以通过 UID FETCH 执行 UID 到序列:

A001 UID FETCH 82342 UID
* FETCH 83 (UID 82342)
A001 OK UID FETCH completed

Assuming that "sorting the mailbox messages by date, newest first" refers to the time the messages were added to the mailbox rather than INTERNALDATE or the Date header:

A001 SEARCH UID 82342
* SEARCH 83
A001 OK SEARCH completed

A002 FETCH 82,84 UID
* FETCH 82 (UID 82309)
* FETCH 84 (UID 82343)
A002 OK FETCH completed

Things to look out for:

  • If the UID doesn't exist in the mailbox, SEARCH will return no results.
  • If the UID matched the first/last message in the mailbox, don't try to fetch the one before/after or you'll get a BAD response.

If you'd prefer not to use SEARCH, you could also do the UID-to-sequence via UID FETCH:

A001 UID FETCH 82342 UID
* FETCH 83 (UID 82342)
A001 OK UID FETCH completed
屋檐 2024-10-04 00:33:52

我认为唯一的方法是首先询问所有 UID:

UID SEARCH ALL

...然后排序。添加到邮箱的每条消息都会分配一个比之前添加的消息更高的 UID。

所以更高的 UID = 更新的电子邮件。

I think the only way would be to ask for all UIDs first:

UID SEARCH ALL

...and sort. Each message added to the mailbox is assigned a higher UID than the messages which were added previously.

So higher UID = newer email.

巷子口的你 2024-10-04 00:33:52

如果你想搜索 12345 之前的 UID,这个命令可以做到:

a UID SEARCH RETURN (MAX) UID 1:12344

查找下一个:

b UID SEARCH RETURN (MIN) UID 12346:*

这需要 ESEARCH 扩展,大多数现代服务器都实现。值得注意的是 gmail 实现了它。不过,循环发出的成本有点太高了,所以也许您最好以其他方式解决问题。以下是获取 12345 之前的 50 个 UID 的两个命令序列:

c1 SEARCH UID 12345
* SEARCH 1000
c1 OK done
c2 UID SEARCH 950:999
* SEARCH 12200,12202,...
c2 OK done

950 是“c1 - 50 的结果”,999 是“c1 - 1 的结果”。

If you want to search for the UID before 12345, this command will do it:

a UID SEARCH RETURN (MAX) UID 1:12344

Finding the next after:

b UID SEARCH RETURN (MIN) UID 12346:*

This requires the ESEARCH extension, which most modern servers implement. Notably gmail implements it. It's a little too expensive to issue in a loop, though, so perhaps you're better off solving your issue in another way. Here's a two-command sequence to get the 50 UIDs immediately preceding 12345:

c1 SEARCH UID 12345
* SEARCH 1000
c1 OK done
c2 UID SEARCH 950:999
* SEARCH 12200,12202,...
c2 OK done

950 is "result of c1 - 50", 999 is "result of c1 - 1".

杀お生予夺 2024-10-04 00:33:52

我不熟悉确切的命令,但你检查过 RFC 吗?

https://www.rfc-editor.org/rfc/rfc3501

看起来像那里是您可以发送的 UIDNEXT 命令,它为您提供下一个 UID。

I'm not familiar with the exact commands, but have you checked out the RFC?

https://www.rfc-editor.org/rfc/rfc3501

Looks like there is a UIDNEXT command you can send that gives you the next UID.

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