'How to get latest messages from Outlook

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!



Solution 1:[1]

While not a direct answer, take a look at Exchange Web Services Managed API 1.2 SDK http://msdn.microsoft.com/en-us/library/dd633710(v=EXCHG.80).aspx. It is much easier to deal with, is so much more flexible, and with no dependency on Outlook.

Working with search by using the EWS Managed API http://msdn.microsoft.com/en-us/library/dd633671(v=exchg.80).aspx

Working with search filters by using the EWS Managed API http://msdn.microsoft.com/en-us/library/dd633659(v=exchg.80).aspx

Look for a MAPI Viewer. Microsoft provides one and there are others. With the viewer, you can look at the folder and item properties to see how you need to filter.

Yet, I think the examples in Working with search filters by using the EWS Managed API will get you going.

Solution 2:[2]

For those who come across this question, a similar one was asked and answered here: Restrict Outlook Items by Date

This is the documentation page: https://docs.microsoft.com/en-us/office/vba/api/Outlook.Items.Restrict

The date has to be formatted like so: M/d/yy h:mm tt (in .NET date format string) Example: May 6, 2007 at 8:09 AM becomes 5/6/07 8:09 AM Full filter string then becomes [ReceivedTime] >= '5/6/07 8:09 AM' (single quotes are important)

Solution 3:[3]

An example how I fixed this using the documentation that also Mr. TA refers too:

            string filterDateString = lastUpdate.Day + "/" + lastUpdate.Month + "/" + lastUpdate.Year + " " + filterDate.ToShortTimeString();
            string filter = "[ReceivedTime] > '" + filterDateString + "'";

            Application outlookApplication = new Application();
            NameSpace outlookNamespace = outlookApplication.GetNamespace("MAPI");
            MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Items mailItems = inboxFolder.Items.Restrict(filter);

EDIT: filterDate.ToShortTimeString() only works in the right way if your CultureInfo is ("en-us") otherwise you need to add AM or PM or do this:

CultureInfo us = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = us;

Solution 4:[4]

Why not connecting directly to your mail server and issuing POP3 or IMAP commands through a simple connection ? I used to do that directly in telnet ages ago. Commands are pretty simple and should work also under windows. (for ref see this)

Another way may be opening directly the Outlook folder, but this will imply that a client version of Outlook is actually running and downloading messages from the server.

Btw, a program which tells you if you've got new mail already exists, is called biff (and I suspect it exists even on MS platform).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AMissico
Solution 2 Mr. TA
Solution 3
Solution 4 BigMike