Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exchange Web Services: Finding emails sent to a recipient

I'm using Exchange Web Services to connect to a mailbox and look for messages matching certain criteria, using FindItems with a SearchFilter.

I can get emails in a mailbox filtering on 'from' email address like this:

var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
            {
                TraceEnabled = true,
                Credentials = new WebCredentials(username, password)
            };

var filter = new SearchFilter.ContainsSubstring(EmailMessageSchema.From, "[email protected]");

service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50))

And I can filter on the DisplayTo property like this:

var filter = new SearchFilter.ContainsSubstring(EmailMessageSchema.DisplayTo, "display name");

But as far as I can tell that only searches the recipient's display name. I want to search on an email address or domain name.

This doesn't return results when I would expect it to:

var filter = new SearchFilter.ContainsSubstring(EmailMessageSchema.ToRecipients, "[email protected]");

Is it possible to find all emails where the recipients list contains a specified email address?

like image 816
mattk Avatar asked Aug 08 '13 13:08

mattk


2 Answers

I didn't find a way to use a SearchFilter to find emails based on recipient email address.

It is possible using a different overload of ExchangeService.FindItems which takes a querystring.

Finding emails where an address is in the To or Cc fields

var contactEmailAddress = "[email protected]";

var querystring = string.Format("Participants:={0}", contactEmailAddress);

service.FindItems(WellKnownFolderName.Inbox, queryString, view);

Finding emails where an address is in the From, To or Cc fields

var contactEmailAddress = "[email protected]";

var querystring = string.Format("(From:={0} OR Participants:={0})", contactEmailAddress);

service.FindItems(WellKnownFolderName.Inbox, queryString, view);

I think this feature requires Exchange 2010.

Some additional resources on query syntax:

  • QueryString (QueryStringType) [MSDN]
  • Using Exchange Search and AQS with EWS on Exchange 2010
like image 170
mattk Avatar answered Sep 23 '22 22:09

mattk


It might be because you don't access the correct folder, ie: sent items.

Replace

service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(50))

By

service.FindItems(WellKnownFolderName.SentItems, filter, new ItemView(50))

Edit: I misunderstood the initial question. Maybe you should have a look at the following MSDN blog: http://blogs.msdn.com/b/akashb/archive/2010/03/05/how-to-build-a-complex-search-using-searchfilter-and-searchfiltercollection-in-ews-managed-api-1-0.aspx It explains how to make complex searches using EWS

like image 23
Fabien Avatar answered Sep 22 '22 22:09

Fabien