Search code examples
c#outlookoffice-interop

How can i loop through a mailbox with restrictive criteria?


I see many questions on how to loop through mails which i can quite easily acheive. but what i'm struggling with is restricting the collection so that i only iterate on a smaller selection of emails based on crtieria for example date.

Whats possible here? Linq? Lambda? Find/Restrict? - whats most efficient?

Here is what i've tried so far but i'm struggling to get anything, the collection is returning null: (the query is null reference not set to reference or object)

string sFilter = $"[ReceivedTime] > {DateTime.Today.AddDays(-7).ToString()}";
            Outlook.Items query = itemsIn.Find(sFilter);```


Solution

  • Items.Find/FindNext and Items.Restrict are most efficient: they are implemented on the store provider level.

    Linq or Lambda might look good in the source code, but they still expliiiclty iterate through all items in the collection. Don't use then with Outlook's Items collections.