+2 votes

Hi,

I'd like know if and how your product can get Gmail emails between certain dates (search the Gmail folder by dates). Also if and how your product can get Gmail emails by keywords contained in the email body.

by

1 Answer

0 votes
 
Best answer

This article explains how to search IMAP using Mail.dll:
https://www.limilabs.com/blog/how-to-search-imap-in-net

For example searching in email's body looks like this:

List<long> uids = imap.Search(
   Expression.Body("Hi"));

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.

For example:

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

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(
        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();
}

Additionally Gmail offers searching using Gmail UI syntax:
https://www.limilabs.com/blog/search-gmail-using-gmails-search-syntax

List<long> uids = imap.Search(
        Expression.GmailRawSearch("after:2004/04/16 before:2004/04/18");

Meaning: Messages sent between April 16, 2004 and April 18, 2004.
(Messages sent after 12:00 AM (or 00:00) April 16, 2004 and before April 18, 2004).

This syntax is available for Gmail only, it is described here:
https://support.google.com/mail/answer/7190

by (298k points)
...