I’m writing a work management application in C# for my team at work (because our stupid employer didn’t give us any appropriate tools). I need to check for new messages in Outlook every once in a while and I need to get messages since the last time I checked them.
I’ve tried the Items.Restrict() method and used the filter "[ReceivedTime] > ' "+lastUpdate+" ' ", but it doesn’t seem to work correctly. I’ve probably tried every possible format of lastUpdate – converted to universal time, to string, to US date/time format. NOTHING works correctly. It either gives me messages in absolutely different time range or doesn’t find any msgs at all.
Any idea what I’m doing wrong? I’m also thinking about using AdvancedSearch() but will it be easy and quick to implement? Thanks in advance for your replies!
OK, I’ve wasted some more time on this and found a simplier solution which doesn’t force me to rewrite my whole application from scratch.
Instead of using the
Items.Restrict()method to get the latest messages I’m usingItems.Sort()to sort them by ReceivedTime and then loop through the first messages from the Items list until I find a message which is older than my last update timestamp.Example:
This solution, unfortunately, has a drawback because
Items.Sort()seems to work only in Exchange mailbox (doesn’t sort properly in ordinary POP mailbox) but I need it only for Exchange anyway, so it’s enough for me.I think, it’s also possible to use Outlook’s AdvancedSearch or MAPI tables but I haven’t tried that.