简体   繁体   English

如何使用MailKit以相反的顺序获取UID列表?

[英]How to get a list of UIDs in reverse order with MailKit?

I would like to get the latest 100 UIDs from the inbox using MailKit.我想使用 MailKit 从收件箱中获取最新的 100 个 UID。 I am accessing a Gmail mailbox which does not appear to support the SORT extension so I am unable to use OrderBy.我正在访问一个似乎不支持 SORT 扩展的 Gmail 邮箱,因此我无法使用 OrderBy。

Here is my code.这是我的代码。 The problem is that it appears to retrieve the oldest 100 emails rather the latest ones (which is how I would expect it to work).问题是它似乎检索最旧的 100 封电子邮件,而不是最新的(这是我希望它工作的方式)。 Is there a way to do this?有没有办法做到这一点?

Option A - looks promising only gets the 100 oldest emails UIDs and I want the 100 newest:选项 A - 看起来很有希望只获取 100 个最旧的电子邮件 UID,而我想要 100 个最新的:

            imap.Inbox.Open(FolderAccess.ReadOnly);
            var orderBy = new [] { OrderBy.ReverseArrival };
            var items = imap.Inbox.Fetch(0, limit, MessageSummaryItems.UniqueId);

Option B - gets all UIDs by date order (but does not work on Gmail anyway):选项 B - 按日期顺序获取所有UID(但无论如何不适用于 Gmail):

            imap.Inbox.Open(FolderAccess.ReadOnly);
            var orderBy = new [] { OrderBy.ReverseArrival };
            SearchQuery query = SearchQuery.All;
            var items = imap.Inbox.Search(query, orderBy);

The IMAP server does not support the SORT extension. IMAP 服务器不支持 SORT 扩展。

The reason is to quickly scan the mailbox in order to improve responsiveness to user.原因是快速扫描邮箱以提高对用户的响应能力。

You were pretty close in Option A, you just used the wrong values for the first 2 arguments.您在选项 A 中非常接近,您只是对前 2 个参数使用了错误的值。

This is what you want:这就是你想要的:

imap.Inbox.Open (FolderAccess.ReadOnly);
if (imap.Inbox.Count > 0) {
    // fetch the UIDs of the newest 100 messages
    int index = Math.Max (imap.Inbox.Count - 100, 0);
    var items = imap.Inbox.Fetch (index, -1, MessageSummaryItems.UniqueId);
    ...
}

The way that IMailFolder.Fetch (int, int, MessageSummaryItems) works is that the first int argument is the first message index and the second argument is the last message index in the range ( -1 is a special case that just means "the last message in the folder"). IMailFolder.Fetch (int, int, MessageSummaryItems)工作方式是第一个 int 参数是第一个消息索引,第二个参数是范围内的最后一个消息索引( -1是一个特殊情况,仅表示“最后一个文件夹中的消息”)。

Since once we open the folder, we can use the IMailFolder.Count property to get the total number of messages in the folder, we can use that to count backwards from the end to get our starting index.因为一旦我们打开文件夹,我们就可以使用IMailFolder.Count属性来获取文件夹中的邮件总数,我们可以使用它从末尾向后计数以获得我们的起始索引。 We want the last 100, so we can do folder.Count - 100 .我们想要最后 100 个,所以我们可以执行folder.Count - 100 We use Math.Max() to make sure we don't get a negative value if the number of messages in the folder is less than 100.如果文件夹中的邮件数小于 100,我们使用Math.Max()来确保我们不会得到负值。

Hope that helps.希望有帮助。

If you want to download each Message individually, you can do something simple like this.如果你想单独下载每个消息,你可以做一些像这样简单的事情。

for (int i = inbox.Count-1; i > inbox.Count-101; i--)
{
    var message = inbox.GetMessage(i);
    Console.WriteLine($"Subject: {message.Subject}");
}

If you would like to receive it all in one request, try this.如果您想在一个请求中接收所有内容,请尝试此操作。

var lastHundredMessages = Enumerable.Range(inbox.Count - 100, 100).ToList();
var messages = inbox.Fetch(lastHundredMessages, MailKit.MessageSummaryItems.UniqueId);
foreach (var message in messages)
{
    //To something here with this
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM