10

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, "some@email.com");

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, "some@email.com");

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

mattk
  • 1,335
  • 1
  • 14
  • 19
  • Looks like this issue still exist in EWS API 2.2. Have you found any workaround? – 02Anant Apr 10 '15 at 15:24
  • @02Anant My solution was to use FindItems with a query string, shown in my answer and described here https://msdn.microsoft.com/en-us/library/ee693615.aspx – mattk Apr 15 '15 at 11:59

3 Answers3

8

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 = "some@email.com";

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 = "some@email.com";

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:

mattk
  • 1,335
  • 1
  • 14
  • 19
6

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

Fabien
  • 1,015
  • 2
  • 11
  • 22
  • 1
    That's not the problem. The inbox folder contains emails which need to be retrieved using EWS. It is possible to find emails in the inbox but not to filter them by recipient address when using the SearchFilter API. – mattk Sep 04 '13 at 15:48
  • Thanks @Fabien, Yes I then figured out that INBOX=>SENTITEMS problem, But I still can't filter out the emails sent to specific recipient from SentItems folder. I checked your link, It gives facility to search by ==, !=, <, > etc but not like in_array. I need here something like to check if an emailaddress exists in RecipientsArray. can you please guide me. – Maulik Vora Sep 05 '13 at 11:59
  • This doesn't work regardless of the actual folder. Using the query-string overload seems to be the only viable option at the moment - hopefully this gets fixed by the EWS team at some point – Hershi Mar 15 '15 at 22:01
1

Note that you cannot extend the FindItems method or the FindItem operation to retrieve additional properties and elements such as ToRecipients, CcRecipients, and BccRecipients. If you need to retrieve those values, use the FindItems method or the FindItem operation to get the item IDs of the emails, and then use the Bind method or the GetItem operation, to retrieve the required properties. Ref: MSDN Email properties

Here is how I did it:

 ItemView view = new ItemView(200);
    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.DateTimeSent);

    List<SearchFilter> searchFilterCollection = new List<SearchFilter>();   
    searchFilterCollection.Add(new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeSent, LastServiceRun)); //Fetching recently sent emails 
    //In case you want to have more than one filter    
    SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);

    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.SentItems, searchFilter, view);

    foreach (Microsoft.Exchange.WebServices.Data.EmailMessage item in results)
    {
        PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.ToRecipients);

        EmailMessage sentEmail = (EmailMessage)Item.Bind(service, item.Id, propSet);

        if (sentEmail.ToRecipients.Any(sEmail => sEmail.Address == mySearchEmailAddress))
        {
            //An email found where an address is in the To field - Your logic comes here
        }

    }`
Ryan
  • 615
  • 8
  • 19