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?
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("[email protected]");
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.