Click here to Skip to main content
11,410,529 members (61,268 online)
Click here to Skip to main content

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

, 18 Feb 2004 Public Domain
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.

Comments and Discussions

Questioncode to send mail using outlook profile Pinmemberkrishnapp5-Aug-11 1:17 
GeneralMy vote of 2 Pinmemberr.balag1-Jul-10 22:41 
QuestionHow do I get the contents of the emails? Pinmembervincent901529003323-Nov-08 22:21 
GeneralChecking another profile PinmemberAdam Cheeseman27-Aug-08 6:36 
GeneralRe: Checking another profile PinmemberAdam Cheeseman27-Aug-08 6:38 
GeneralOutlook Security??!! PinmemberVasya - dragon20-Apr-06 14:08 
GeneralMAPI E LOGON FAILED PinmemberMIRo2k27-Aug-05 23:06 
GeneralRe: MAPI E LOGON FAILED Pinmemberwguerram17-Jan-06 7:35 
GeneralCOM error Pinmemberjoerage20-Jul-05 12:44 
GeneralRe: COM error PinmemberPartyking27-Apr-07 0:57 
GeneralRe: COM error PinmemberAdam Cheeseman27-Aug-08 6:30 
GeneralRe: COM error PinmemberJubjub19-Dec-08 22:10 
GeneralConsole app PinmembergavinJeffrey19-Oct-04 23:21 
GeneralClient side add-in that does the same Pinmemberurielm24-Feb-04 6:20 

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

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

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