Filter and Sort Messages in Exchange Server Mailbox Using EWS

Filter and Sort 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.

Filter Messages by Criteria

To get filtered messages from a mailbox:

  1. Connect to the Exchange server.
  2. Create an instance of MailQuery and set the desired properties.
  3. 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.

By Today’s Date

The following code snippet shows you how to filter all emails on the basis of today’s date.

By Date Range

The following code snippet shows you how to filter all emails on the basis of the date range.

By Sender

The following code snippet shows you how to filter all emails on the basis of a specific sender.

By Domain

The following code snippet shows you how to filter all emails on the basis of a specific domain.

By Recipient

The following code snippet shows you how to filter all emails on the basis of a specific recipient.

By MessageID

The following code snippet shows you how to filter all emails on the basis of MessageID.

By Mail Delivery Notifications

The following code snippet shows you how to filter all emails on the basis of all mail delivery notifications.

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 “noreply@host.com” 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.

Filter Messages with Paging Support

Sort 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.

Filter Messages Using AQS

With Aspose.Email for .NET, users can leverage the powerful capabilities of Advanced Query Syntax (AQS) to filter messages directly from an Exchange mailbox. AQS provides a robust and intuitive means of constructing queries that can precisely target emails based on specific criteria such as date, sender, and subject. For more detailed insights on its itegration into your email filtering processes and comprehensive code samples on implementing message filtering using AQS with Aspose.Email for .NET, please refer to the Filter Messages With AQS From Exchange Mailbox article.