Filter Messages From Exchange Mailbox
Filtering Messages using EWS
The IEWSClient interface provides the ListMessages() method which gets all messages from a mailbox. To get only messages which match some condition, use the overloaded ListMessages() method which takes the MailQuery class as an argument. The MailQuery class provides various properties for specifying conditions, for example, date, subject, sender and recipient. In addition, the API also allows applying case-sensitivity filters for retrieving emails from the mailbox.
To get filtered messages from a mailbox:
- Connect to the Exchange server.
- Create an instance of MailQuery and set the desired properties.
- Call the IEWSClient.ListMessages() method and pass the MailQuery in the parameters to get the filtered messages only.
The following code snippet shows you how to connect to an IMAP mailbox and get messages that have the string “Newsletter” in the subject and were sent today.
Filter Messages on Criteria
The code samples above filters messages based on the email subject and date. We can filter on other properties too. Below are some examples of setting the conditions using MailQuery.
Filter Criteria Today’s Date
The following code snippet shows you how to filter all emails on the basis of today’s date.
Filter Criteria Date Range
The following code snippet shows you how to filter all emails on the basis of the date range.
Filter Criteria Specific Sender
The following code snippet shows you how to filter all emails on the basis of a specific sender.
Filter Criteria Specific Domain
The following code snippet shows you how to filter all emails on the basis of a specific domain.
Filter Criteria Specific Recipient
The following code snippet shows you how to filter all emails on the basis of a specific recipient.
Filter Criteria By MessageID
The following code snippet shows you how to filter all emails on the basis of MessageID.
Filter Criteria All Mail Delivery Notifications
The following code snippet shows you how to filter all emails on the basis of all mail delivery notifications.
Filter by Message Size
Building Complex Queries
If different MailQueryBuilder properties are set in a separate statement, all the conditions are matched. For example, to get a message in a particular date range and from a specific host, 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 messages that match any of the two conditions specified. The example below filters messages that either have the word “test” in the subject or “firstname.lastname@example.org” as the sender. The following code snippet shows you how to combine queries with OR.
Case-Sensitive Email Filtering
Emails can be filtered based on case-sensitivity by specifying the IgnoreCase flag in the filter criteria as shown in the following code snippet.
Filtering Messages with Paging Support
Sorting filtered messages in ascending/descending order
Email filtering can be supported with sorting of messages in ascending/descending order. In this case, OrderBy method is used to specify the order in which the results of an email search are sorted using the MailQueryBuilder class. This method allows you to define sorting criteria for a search query, specifying whether the results should be sorted in ascending or descending order based on a particular property.
The method accepts the ascending parameter, which specifies the sort order for the specified property. If the ascending parameter is true, it means that the search results should be sorted in ascending order. Conversely, if the ascending parameter is false, it means that the search results should be sorted in descending order.
MailQueryBuilder builder = new MailQueryBuilder(); builder.Subject.Contains("Report"); builder.InternalDate.Since(new DateTime(2020, 1, 1)); builder.Subject.OrderBy(true); // sort the subject ascending builder.InternalDate.OrderBy(false); // sort the date descending MailQuery query = builder.GetQuery(); // Get list of messages ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query, false);
In the above code snippet, the OrderBy method is applied twice, once for the subject and once for the date of the emails. As a result of executing the ListMessages method with the passed request, we will get a list of messages with the subject containing the word “Report” which were received on the specified date or later. At the same time, the results will be sorted by subject in ascending order. This means that the messages will be sorted alphabetically from A to Z, depending on their subject. Also, the results will be sorted by date in descending order. This means that posts will be ordered from newest to oldest.