Pergunta

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.

Foi útil?

Solução

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top