Exchange Web Services: Finding emails sent to a recipient

拜拜、爱过 提交于 2019-12-05 02:47:31

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:

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

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
        }

    }`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!