Filter Messages from Server using IMAP Client
The ImapClient class provides the ListMessages() method which gets all the messages from a mailbox. To get only messages which match some condition, use the overloaded ListMessages() method which takes MailQuery as an argument. The MailQuery class provides various properties for specifying the conditions, for example, date, subject, sender, recipient and so on. The first example illustrates how to filter messages based on date and subject. We also show how to filter on other criteria and how to build more complex queries. The API also provides the capability to apply case-sensitive searching criteria to match exact filtering criteria. The API also allows specifying the search string encoding for filtering messages from the mailbox.
Filtering Messages from Mailbox
- Connect and log in to an IMAP server
- Create an instance of the MailQuery and set the properties
- Call the
ImapClient.ListMessages(MailQuery query)
method and pass theMailQuery
with the parameters to get filtered messages only.
The following code snippet shows you how to connect to an IMAP mailbox and get messages that arrived today and have the word “newsletter” in the subject.
Get Messages that Meet Specific Criteria
The code samples above filters messages based on the email subject and date. We can use other properties to set other supported conditions as well. Below are some examples of setting the conditions using MailQuery. The code snippets that follow show how to filter emails on:
- Today’s date.
- A date range.
- From a specific sender.
- From a specific domain.
- From a specific recipient.
Today’s Date
The following code snippet shows you how to filter emails on Today’s Date.
Date Range
The following code snippet shows you how to filter emails on the date range.
Specific Sender
The following code snippet shows you how to filter emails on a specific sender.
Specific Domain
The following code snippet shows you how to filter emails on a specific domain.
Specific Recipient
The following code snippet shows you how to filter emails on a specific recipient.
Building Complex Queries
If different MailQueryBuilder properties are set in separate statements, then all the conditions would be matched. For example, if we want to get messages between a date range and from a specific host, we need to write three statements.
Combining Queries with AND
The following code snippet shows you how to combine queries with AND.
Combining Queries with OR
MailQueryBuilder provides the Or() method which takes two MailQuery instances as parameters. It gets the messages that match any of the two conditions specified. The following code snippet shows how to filter messages that either have “test” in the subject or “noreply@host.com” as the sender. The following code snippet shows you how to combine queries with OR.
Filtration on InternalDate
Messages can be extracted from the server based upon the InternalDate however sometimes the server does not return all messages as visible in the inbox. Its reason can be the server timezone because it may not be UTC for all the servers like Gmail. Aspose sends commands like 008 SEARCH ON 4-May-2014 as per the IMAP protocol however result can differ due to server timezone settings. A new member is added in ImapMessageInfo as InternalDate which further helps in filtering the messages. The following code snippet shows the use of InternalDate to filter messages.
Case-Sensitive Emails Filtering
The following code snippet shows you how to use case-sensitive emails filtering.
Specify Encoding for Query Builder
The API’s ImapQueryBuilder constructor can be used to specify the Encoding for the search string. This can also be set using the DefaultEncoding property of the MailQueryBuilder. The following code snippet shows you how to specify the encoding for query builder.
Filter Messages with Paging Support
The ImapClient provides the capability to search for messages from the mailbox and list them with paging support. The following code snippet shows you how to filter messages with paging support.
Filter Messages with Custom Flag
Filter Messages using Custom Search
For example, RFC 3501 standard does not allow a message search based on the existence of attachments in messages. But Gmail provides IMAP Extensions that allow performing such a search. The next code snippet shows how to make a corresponding query.
ImapQueryBuilder queryBuilder = new ImapQueryBuilder();
queryBuilder.CustomSearch("X-GM-RAW \"has:attachment\"");
MailQuery mailQuery = queryBuilder.GetQuery();
ImapMessageInfoCollection messageInfoCollection = imapClient.ListMessages(mailQuery);