Exchange.ListMessages returns nothing (C#)

I’m working with Exchange mail client. While I’m filtering mail by To.Address field it doesn’t work.
Filtering by To.DisplayName - works fine.

In addition, I have the same problem while I’m filtering by From fields but in the opposite:
While I’m filtering mail by From.Address field it works well.
Filtering by From.DisplayName - doesn’t work.

Here below my snippet code:
Creating Query:
MailQuery mq = null;
ExchangeQueryBuilder qb = new ExchangeQueryBuilder();
qb.Or(qb.From.Contains(pFromAddress), qb.From.Contains(pFromName));
mq = qb.GetQuery();
return mq;

Create mail client:
NetworkCredential credentials = new NetworkCredential(UserName, Password, Domain);
IEWSClient MailClient = EWSClient.GetEWSClient(ServerHost, credentials);

Get mails by query:
MailClient.ListMessages(pSubfolderUri, pMaxResult, pMailQuery); --> this line returns 0 mails count although there is a mail matched to query.

Note that if I’m testing this code while I’m using IMAP server - I got results.

Additional info:
Working with Aspose.Email version
I tried it on IMAP server in office365 -
Unfortunately, I can’t attach user & password to tested mail.



I have observed the issue shared by you and request you to please first try using latest Aspose.Email for .NET 19.11 on your end. In case the issue is still reproduced, please share the working sample project reproducing the issue on your end along with test account access.

Thanks for your reply.
I tried to test this issue in 19.11 Aspose.Email but it still occurs.

Attached my project (I change version to be 19.6 which is the version we are work with. (503.7 KB)



I have worked with the sample project shared. An issue with ID EMAILNET-39686 has been created in our issue tracking system to investigate the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

Thank you so much :slight_smile:


You are always welcome :slight_smile:.

Hi, sorry to raise an old complain but i’m experiencing the same problem in 20.5, was this ever fixed?


The issue associated in this thread has been marked as resolved in upcoming Aspose.Email for .NET 20.6. We have added a search option by using Advanced Query Syntax (AQS). This feature will solve the issue.

New API members

ExchangeAdvancedSyntaxQueryBuilder class - represents the builder of search expression based on the Advanced Query Syntax (AQS)
ExchangeAdvancedSyntaxMailQuery class - implements an Advanced Query Syntax (AQS) search that is used by EWS.

AQS described at Perform an AQS search by using EWS in Exchange | Microsoft Learn.

Code examples

Create a search query using query builder builder:

using (IEWSClient client = EWSClient.GetEWSClient(exchangeMailboxUri, name, password))
    ExchangeAdvancedSyntaxQueryBuilder advancedBuilder = new ExchangeAdvancedSyntaxQueryBuilder();
    advancedBuilder.From.Contains("Lindsey Smart");
    //advancedBuilder.To.Contains("Jim Martin");

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

Сreate the search query directly by using Advanced Query Syntax (AQS):
See the AQS description at Perform an AQS search by using EWS in Exchange | Microsoft Learn.

using (IEWSClient client = EWSClient.GetEWSClient(exchangeMailboxUri, name, password))
    ExchangeAdvancedSyntaxMailQuery query = new ExchangeAdvancedSyntaxMailQuery("( AND (");
    ExchangeMessageInfoCollection messages = client.ListMessages(client.MailboxInfo.InboxUri, query);
1 Like

The issues you have found earlier (filed as EMAILNET-39686) have been fixed in this update.