Filter Messages From Exchange Mailbox

Aspose.Email for C++ allows developers to filter messages in an Exchange mailbox using IEWSClient, MailQuery, and ExchangeQueryBuilder. You can filter messages by date, sender, domain, MessageID, delivery notifications, and many other criteria.

To retrieve messages from a folder for further processing, the IEWSClient. has the following methods:

  • ListMessages() - Gets all messages from a mailbox.
  • ListMessages() overload - Returns only messages that match specific conditions. It accepts a MailQuery which defines filtering rules such as subject keywords, date ranges, and address-based filtering.

Filter Messages with IEWSClient

The following code sample demonstrates how to query and retrieve specific emails from an Exchange Server using Exchange Web Services (EWS) with Aspose.Email for C++. It shows the complete process of connecting to an Exchange server (Office 365 in this case), building a search query to find messages with “Newsletter” in the subject that arrived today, executing the query against the inbox, retrieving the matching messages, and properly handling the connection lifecycle with error handling.

  1. Connect to the Exchange server using IEWSClient.
  2. Create a MailQuery or ExchangeQueryBuilder and define filtering conditions.
  3. Call ListMessages(folderUri, query) to get the filtered results.

Filter Messages on Criteria

The code sample above filters messages based on the email subject and date. You can filter on other properties too. Below are some examples of setting the conditions using MailQuery.

Filter by Today’s Date

The following code sample demonstrates how to build a query to find emails that arrived today.

Filter by Date Range

The following code sample demonstrates how to build a query to find emails that arrived within the last 7 days.

Filter by Specific Sender

The following code sample demonstrates how to build a query to find emails from a specific sender.

Filter by Domain

The following code sample demonstrates how to build a query to find emails from a specific domain.

Filter by Recipient

The following code sample demonstrates how to build a query to find emails sent to a specific recipient.

Filter by MessageID

The following code sample demonstrates how to build a query to find a specific email by its MessageId.

Filter Mail Delivery Notifications

The following code sample demonstrates how to build a query to find Mail Delivery Notifications (MDNs).

Filter by Message Size

The following code sample demonstrates how to build a query to find emails larger than a specific size.

Build Complex Queries

When using MailQueryBuilder or ExchangeQueryBuilder, each property you set creates a filtering condition. If these conditions are defined in separate statements, they are combined using logical AND, meaning all conditions must match for a message to be returned.

This allows developers to build precise filters such as:

  • Emails within a date range
  • Emails from a specific domain
  • Emails matching multiple criteria simultaneously

Combine Queries with AND

Using multiple builder properties in sequence automatically creates an AND operation.

The following example retrieves messages that:

  • Come from a specific domain
  • Arrived before today
  • Arrived within the last seven days

Combine Queries with OR

To match messages that satisfy either of two conditions, use the Or() method.

The following example gets messages that:

  • Contain “test” in the subject, or
  • Were sent by “noreply@host.com”

Case-Sensitive Email Filtering

You can apply case-sensitive or case-insensitive filtering when querying messages from an Exchange mailbox. To control this behavior, use the IgnoreCase flag available in the filtering methods. Passing true enables case-insensitive matching.

The example below filters messages that:

  • Contain the word “Newsletter” in the subject (case-insensitive)
  • Arrived today

Pagination in Message Filtering

When working with large Exchange mailboxes, paging allows you to retrieve messages in smaller, manageable batches. The ListMessagesByPage method of IEWSClient returns results page-by-page based on a specified page size and query criteria.

The example below demonstrates how to:

  1. Build a filter using MailQueryBuilder.
  2. Retrieve results in pages.
  3. Loop through all pages until the last one.
  4. Count the total number of matching messages.