0 votes

How can I search mail in two date in imap

by

1 Answer

0 votes

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

  • Since - Creates criterion to find emails whose internal date (assigned by IMAP server) (disregarding time and timezone) is within or later than the specified date.

  • Before - Creates criterion to find emails whose internal date (disregarding time and timezone) is earlier than the specified date.

  • SentSince - Creates criterion to find emails whose [RFC-2822] Date: header (disregarding time and timezone) is within or later than the specified date.

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

Here's the sample code that find mails which where received in 2014:

using (Imap imap = new Imap())
{
    imap.ConnectSSL("imap-mail.outlook.com");
    imap.UseBestLogin(_session.User, _session.Password);

    List<long> uids = imap.Search().Where(
        Expression.And(
            Expression.Since(new DateTime(2014,1,1)),
            Expression.Before(new DateTime(2014,12,31))
            ));

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

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

    imap.Close();
}

References:
https://www.limilabs.com/blog/how-to-search-imap-in-net

by (297k points)
...