仅从IMAP服务器获取新邮件 [英] Getting only new mail from an IMAP server

查看:359
本文介绍了仅从IMAP服务器获取新邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个从IMAP服务器中获取电子邮件的客户端应用程序,然后将其存储在数据库中。问题是,一旦我检查了邮件,下次我只想下载已经到达的邮件。所以如果我在两小时之前检查过邮件服务器,我只想收到最近两个小时内到达的邮件。

I am writing a client application that fetches emails from an IMAP server and then stores them in a database. The problem is that once I have checked the mail, the next time I only want to download the mail that has arrived since. So if I had checked the server for mail two hour ago, I only want to get the mail that has arrived in the last two hours.

我可以使用SEARCH DATE,但是没有时间的支持+日期可能很容易被欺骗。

I could use SEARCH with SINCE DATE, but there's no support for time + date could be easily spoofed.

我也尝试过RECENT标志,但这似乎并不适用于gmail(在ruby

I also tried the RECENT flag, but that doesn't seem to work with gmail (in ruby it shows nil everytime).

推荐答案

您要使用UniqueId(UID)作为消息。这就是为什么创建它。

You want to use the UniqueId (UID) for the messages. This is specifically why it was created.

您将要跟踪最后一个UID请求,然后请求所有使用消息集的新消息[ UID]:*,其中[UID]是实际的UID值。

You will want to keep track of the last UID requested, and then, to request all new messages you use the message set "[UID]:*", where [UID] is the actual UID value.

例如,让我们说最后一条消息的唯一标识是123456。您将获取

For example, lets say the last message feteched had a unique id of "123456". You would fetch

123456:*

然后,丢弃第一个返回的消息。

Then, discard the first returned message.

UID在会话中是稳定的,永远不会改变,并且总是增加价值。验证这一点的抓住是选择文件夹时检查UIDValidity。如果UIDValidity数字没有改变,则UID在会话之间仍然有效。

UIDs are 'supposed' to be stable across sessions, and never change, and always increase in value. The catch to verify this, is to check the UIDValidity when you select the folder. If the UIDValidity number hasn't changed, then the UIDs should still be valid across sessions.

以下是RFC中的相关部分:

Here are the relevant parts from the RFC:

2.3.1.1。唯一标识符(UID)消息属性

2.3.1.1. Unique Identifier (UID) Message Attribute

分配给每个消息的32位值,与
唯一标识符有效值(见下文)一起使用时一个64位的值
,绝对不能引用邮箱中的任何其他消息或任何
后续邮箱与其名称永远相同。唯一标识符
在邮箱中以严格上升的方式分配;因为每个
消息被添加到邮箱,所以它被分配比之前添加的
消息更高的UID。与消息序列
数字不同,唯一标识符不一定是连续的。

A 32-bit value assigned to each message, which when used with the unique identifier validity value (see below) forms a 64-bit value that MUST NOT refer to any other message in the mailbox or any subsequent mailbox with the same name forever. Unique identifiers are assigned in a strictly ascending fashion in the mailbox; as each message is added to the mailbox it is assigned a higher UID than the message(s) which were added previously. Unlike message sequence numbers, unique identifiers are not necessarily contiguous.

消息的唯一标识不能在
会话期间更改,而SHOULD会话之间不改变必须使用下面讨论的
UIDVALIDITY机制来检测会话之间的
唯一标识符的任何更改。持久性唯一标识符
是客户端需要从先前的
会话与服务器重新同步其状态(例如,断开连接或脱机访问
客户端);这在[IMAP-DISC]中进一步讨论。

The unique identifier of a message MUST NOT change during the session, and SHOULD NOT change between sessions. Any change of unique identifiers between sessions MUST be detectable using the UIDVALIDITY mechanism discussed below. Persistent unique identifiers are required for a client to resynchronize its state from a previous session with the server (e.g., disconnected or offline access clients); this is discussed further in [IMAP-DISC].

注意:下一个唯一标识符值旨在为
提供一种客户端确定是否有
邮件已经交付到邮箱,因为
以前的时间检查了此值。

这里是链接更多信息:

http:// www.faqs.org/rfcs/rfc3501.html

我会做什么,还会跟踪已下载的消息的InternalDate。这样,如果您失去了UID同步,则可以至少对邮件进行迭代,并根据消息的InternalDate找到您下载的最后一个。

What I would do, is also keep track of the InternalDate of the messages downloaded. This way, if you ever lose UID sync, you can at least iterate through the messages, and find the last one you downloaded, based upon the InternalDate of the message.

这篇关于仅从IMAP服务器获取新邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆