0 votes

Hy guys,

I'm playing with your email component. My question is: is it possible to download only emails received from a specific date to now?

Thank you,
Attilio

by (400 points)
retagged by

1 Answer

0 votes

Yes, you can do that using .NET IMAP component and expression API:

List<long> uids = imap.Search().Where(
    Expression.SentBefore(DateTime.Now.AddDays(-10))
    );

Here's more detailed how to search IMAP in .NET guide.

There are several 'date' search criterions in IMAP, that allow to search for emails within a specified range.

  • SentSince - Finds emails whose [RFC-2822] Date: header (disregarding time and timezone) is equal or later than the date specified .

  • SentBefore - Finds emails whose [RFC-2822] Date: header (disregarding time and timezone) is earlier than the date specified .

  • Since - Finds emails whose internaldate (assigned by IMAP server on receive) (disregarding time and timezone) is equal or later than the date specified .

  • Before - Finds emails whose internaldate (disregarding time and timezone) is earlier than the date specified.

Entire code would look more or less like this:

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap.example.com");
    imap.UseBestLogin("user@example.com", "password");

    List<long> uids = imap.Search().Where(
        Expression.SentBefore(DateTime.Now.AddDays(-10))
        );

    foreach (long uid in uids)
    {
        IMail email = new MailBuilder().CreateFromEml(
            imap.GetMessageByUID(uid));

        string subiect = email.Subject;
        string text = email.Text;
    }

    imap.Close();
}
by (301k points)
...