0 votes

I want to get latest 40 emails from my Inbox folder descending order by Date or may be uid. Kindly let me know how can I get this.

When I am using Flag.All and foreach (long uid in uids.Take(40)). I am getting 40 emails, but all emails are very old. Not the latest emails.

Kindly redirect me to some C# code sample.

by (8.8k points)

1 Answer

0 votes
 
Best answer

There are 2 ways of doing that.

First use Reverse:

using (Imap client = new Imap())
{
    client.ConnectSSL("imap.example.com");
    client.Login("pat@example.com","password");

    List<long> uids = client.GetAll();
    uids.Reverse();
    List<long> newest40 = uids.Take(40).ToList();

    client.Close();
}

Second which is a bit tricky as it may download less than 40 elements if some emails were deleted (it should be a tiny bit faster as it doesn't download all uids stored in the folder).

FolderStatus status = client.SelectInbox();
List<long> newest20 = client.Search(
    Expression.UID(Range.From(status.UIDNext - 41)));
by (301k points)
selected by
...