Click here to Skip to main content
13,195,564 members (71,432 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 18 Feb 2004

Using Collaboration Data Objects (CDO) to check for new Exchange email

, 18 Feb 2004
Rate this:
Please Sign up or sign in to vote.
Using Collaboration Data Objects (CDO) to check for new Exchange email


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

// New session
oSession = new MAPI.Session();
// Will use vEmpty for Empty parameter
Object vEmpty = Missing.Value;
// Logon
// Get Inbox.
oFolder = (MAPI.Folder)oSession.Inbox;
// Get Messages collection.
MAPI.Messages oMsgs = (MAPI.Messages)oFolder.Messages;
// Get the collection's MessageFilter object
MAPI.MessageFilter oFilter = (MAPI.MessageFilter)oMsgs.Filter;
oFilter.Unread = true; 
// Get messages count.
Object oCount = oMsgs.Count;
// Log off session.
if (oSession != null)

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;
// look for outlook already running
Process[] processes = Process.GetProcessesByName("Outlook");
if (processes.Length == 0)
    // start new outlook process
    Process proc = new Process();
    proc.StartInfo.FileName = "Outlook";
    // pull up the existing outlook window
    IntPtr hWnd=processes[0].MainWindowHandle;
    if (IsIconic(hWnd))
      ShowWindowAsync(hWnd, SW_RESTORE);


My only problem with the code is how much memory it hogs, but I think that's a .NET thing.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Mark F Garrison
Team Leader PartsSource LLC
United States United States
Mark hails from Cleveland, Ohio, USA.

He's a 15 year veteran of software engineering in the defense, medical equipment, automotive, and building automation industries.

He has a wonderful wife and three wonderful children.

You may also be interested in...


Comments and Discussions

Questioncode to send mail using outlook profile Pin
krishnapp5-Aug-11 0:17
memberkrishnapp5-Aug-11 0:17 
GeneralMy vote of 2 Pin
r.balag1-Jul-10 21:41
memberr.balag1-Jul-10 21:41 
QuestionHow do I get the contents of the emails? Pin
vincent901529003323-Nov-08 21:21
membervincent901529003323-Nov-08 21:21 
GeneralChecking another profile Pin
Adam Cheeseman27-Aug-08 5:36
memberAdam Cheeseman27-Aug-08 5:36 
GeneralRe: Checking another profile Pin
Adam Cheeseman27-Aug-08 5:38
memberAdam Cheeseman27-Aug-08 5:38 
GeneralOutlook Security??!! Pin
Vasya - dragon20-Apr-06 13:08
memberVasya - dragon20-Apr-06 13:08 
MIRo2k27-Aug-05 22:06
memberMIRo2k27-Aug-05 22:06 
wguerram17-Jan-06 6:35
memberwguerram17-Jan-06 6:35 
GeneralCOM error Pin
joerage20-Jul-05 11:44
memberjoerage20-Jul-05 11:44 
GeneralRe: COM error Pin
Partyking26-Apr-07 23:57
memberPartyking26-Apr-07 23:57 
GeneralRe: COM error Pin
Adam Cheeseman27-Aug-08 5:30
memberAdam Cheeseman27-Aug-08 5:30 
GeneralRe: COM error Pin
Jubjub19-Dec-08 21:10
memberJubjub19-Dec-08 21:10 
GeneralConsole app Pin
gavinJeffrey19-Oct-04 22:21
membergavinJeffrey19-Oct-04 22:21 
GeneralClient side add-in that does the same Pin
urielm24-Feb-04 5:20
memberurielm24-Feb-04 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171019.1 | Last Updated 19 Feb 2004
Article Copyright 2004 by Mark F Garrison
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid