1

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Nils Anders
  • 4,212
  • 5
  • 25
  • 38
  • Did you TRY anything at all? There's a `SearchFilter` for `FindItems` as described here >> http://msdn.microsoft.com/en-us/library/jj221817(v=exchg.80).aspx – banging Mar 21 '13 at 20:36
  • 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 Mar 21 '13 at 21:18

1 Answers1

12

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);
Kevin Anderson
  • 589
  • 4
  • 17
  • Where do you specify the mailbox? Because one user(credentials) might have multiple mailboxes in the same domain. – erolkaya84 Oct 11 '18 at 09:52