Filter Messages With AQS From Exchange Mailbox

Advanced Query Syntax (AQS) is the query syntax used by Exchange as an alternative to searching filters for expressing search criteria. AQS is a more flexible way to perform searches and deliver search results for all commonly used fields on the items. AQS is also user-friendly, easy to understand and quickly to master. Using AQS is suitable for finding messages by attachments and recipients.

Creating a search query with AQS

You can create a search query with AQS by:

Create a search query using query builder

To create a search query with ExchangeAdvancedSyntaxQueryBuilder you need to:

The code sample below shows how the above steps can be accomplished:

using (var client = EWSClient.GetEWSClient(...))
{
    var advancedBuilder = new ExchangeAdvancedSyntaxQueryBuilder();
    advancedBuilder.From.Equals("Jim Martin");
    advancedBuilder.Subject.Contains("report");
    advancedBuilder.HasAttachment.Equals(true);

    var messages = client.ListMessages(client.MailboxInfo.InboxUri, advancedBuilder.GetQuery());
}

Сreate a search query directly by using AQS:

To create a search query with ExchangeAdvancedSyntaxMailQuery you need to :

The code sample below shows how the above steps can be accomplished:

using (var client = EWSClient.GetEWSClient(...))
{
    ExchangeAdvancedSyntaxMailQuery query = new ExchangeAdvancedSyntaxMailQuery("subject:(product AND report)");
    ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query);
}