Exchange EWS Managed API 2.0 get by date

六眼飞鱼酱① 提交于 2019-12-13 18:07:08

问题


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:


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);


来源:https://stackoverflow.com/questions/15557485/exchange-ews-managed-api-2-0-get-by-date

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