Introduction
This application connects to the Exchange server and checks for unread mail in
your inbox. Outlook does this already, but this feature doesn't work well with SpamBayes. I decided I'd write my own in C#
using the CDO Library
We use the Microsoft CDO 1.21 Library in our project to connect to Exchange,
open the inbox and filter on unread messages as follows
oSession = new MAPI.Session();
Object vEmpty = Missing.Value;
oSession.Logon(vEmpty,vEmpty,false,false,vEmpty,vEmpty,vEmpty);
oFolder = (MAPI.Folder)oSession.Inbox;
MAPI.Messages oMsgs = (MAPI.Messages)oFolder.Messages;
MAPI.MessageFilter oFilter = (MAPI.MessageFilter)oMsgs.Filter;
oFilter.Unread = true;
Object oCount = oMsgs.Count;
if (oSession != null)
oSession.Logoff();
Launching Outlook
I also use some calls from user32.dll to bring outlook up when the system tray
icon is double clicked. This code is from an article written by Marc Clifton on
this website.
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
Process[] processes = Process.GetProcessesByName("Outlook");
if (processes.Length == 0)
{
Process proc = new Process();
proc.StartInfo.FileName = "Outlook";
proc.Start();}
else
{
IntPtr hWnd=processes[0].MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}
SetForegroundWindow(hWnd);
}
Conclusion
My only problem with the code is how much memory it hogs, but I think that's a
.NET thing.