0

I am searching an email inbox (on Exchange 2010) using Exchange Web Services.

In my SearchFilterCollection I would like to include the From.Address property of the Email message, as I only want to retrieve emails that include a certain domain (such as @domain.co.uk)

here is my code:

SearchFilter.SearchFilterCollection searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true));

// **** PROBLEM HERE ON BELOW LINE****       
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.From, "@domain.co.uk")); // 

// add the exceptions
for (int iEx = 0; iEx < e2c.emailExceptions.Count; iEx++)
    {
    searchFilterCollection.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, e2c.emailExceptions[iEx])));
    }

    ItemView view = new ItemView(100);
    view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);

    // Find the first email message in the Inbox that has attachments. This results in a FindItem operation call to EWS.
    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, searchFilterCollection, view);

is there a good way to include the email address in the SearchFilter?

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148

1 Answers1

3

I just created a search folder the other day that did the same thing, using the example from the How to: Work with search folders by using EWS in Exchange topic:

// Create a search filter to express the criteria
// for the folder.
EmailAddress manager = new EmailAddress("sadie@contoso.com");

SearchFilter.IsEqualTo fromManagerFilter =
    new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, manager);

Now this is using the Sender property, not the From property. I think you could simply replace one for the other, but I didn't test to be sure.

Mimi Gentz
  • 1,628
  • 10
  • 14
  • do you think its possible to do something like: `SearchFilter.ContainsSubstring fromManagerFilter = new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, "@bankofcyprus.co.uk");` ? – Our Man in Bananas Apr 11 '14 at 08:30
  • 1
    I would think that would work, but I just tried and got no results when I thought I should have gotten results. But I didn't get an error. I can look at my code a little more this weekend - can't right now. – Mimi Gentz Apr 11 '14 at 22:10
  • 1
    So I didn't get results using the ContainsSubstring search filter when I would have expected to. I did however get the results I expected when I used the following ASQ search: `string queryString = "from:\"domain.co.uk\"";` `FindItemsResults results = service.FindItems (WellKnownFolderName.Inbox, queryString, view);` – Mimi Gentz Apr 14 '14 at 15:03
  • thanks Mimi, so it looks like the only option is replace my EWS SearchFilter collection with a dynamically created AQS query string... – Our Man in Bananas Apr 14 '14 at 15:05
  • any thoughts on this issue: [EWS retrieve mail item attachment error](http://stackoverflow.com/questions/23059919/exchange-web-services-convert-emailitem-attachment-from-base64-string-to-byte) – Our Man in Bananas Apr 14 '14 at 15:05