1
votes

Im using EWS Managed API to communicate between my c# project and our Exchange 2010 server. I use this code to get all mails in the inbox from now and three days back.

    var ews = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    ews.Credentials = new NetworkCredential(usr, psw, dmn);
    ews.AutodiscoverUrl(url);

    PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
    itempropertyset.RequestedBodyType = BodyType.Text;

    ItemView view = new ItemView(int.MaxValue);
    FindItemsResults<Item> findResults;
    view.PropertySet = itempropertyset;

    do
    {
        findResults = ews.FindItems(WellKnownFolderName.Inbox, view);

        foreach (Item item in findResults.Items)
        {
            if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue;

            item.Load(itempropertyset);

            var message = EmailMessage.Bind(ews, item.Id,
                new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));

            string to = message.ToRecipients[0].Address.ToLower();
            string body = item.Body;
        }

        view.Offset += findResults.TotalCount;
    } while (findResults.MoreAvailable);

Now the problem. I want to improve this line if (item.DateTimeCreated < DateTime.Now.AddDays(-3)) continue; because when i use this, the api gets all the messages from inbox and just continue if its older then three days. I want specify this filter earlier in the code, so the api dont have to handle all messages.

1
Did you TRY anything at all? There's a SearchFilter for FindItems as described here >> msdn.microsoft.com/en-us/library/jj221817(v=exchg.80).aspxbanging
Of course, I have tried! Otherwise I would not have asked the question here. You do not seem to have found it either?Nils Anders

1 Answers

11
votes

If I understand the problem correctly, this should work. You can see all search filters available here: EWS Search Filters

ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults;
view.PropertySet = itempropertyset;

SearchFilter searchFilter = 
   new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-3));

findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);