Click here to Skip to main content
14,390,509 members

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

Rate this:
4.28 (16 votes)
Please Sign up or sign in to vote.
4.28 (16 votes)
18 Feb 2004Public Domain
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 Pin
krishnapp5-Aug-11 1:17
Memberkrishnapp5-Aug-11 1:17 
GeneralMy vote of 2 Pin
r.balag1-Jul-10 22:41
Memberr.balag1-Jul-10 22:41 
QuestionHow do I get the contents of the emails? Pin
vincent901529003323-Nov-08 22:21
Membervincent901529003323-Nov-08 22:21 
GeneralChecking another profile Pin
Adam Cheeseman27-Aug-08 6:36
MemberAdam Cheeseman27-Aug-08 6:36 
GeneralRe: Checking another profile Pin
Adam Cheeseman27-Aug-08 6:38
MemberAdam Cheeseman27-Aug-08 6:38 
GeneralOutlook Security??!! Pin
Vasya - dragon20-Apr-06 14:08
MemberVasya - dragon20-Apr-06 14:08 
MIRo2k27-Aug-05 23:06
MemberMIRo2k27-Aug-05 23:06 
wguerram17-Jan-06 7:35
Memberwguerram17-Jan-06 7:35 
GeneralCOM error Pin
joerage20-Jul-05 12:44
Memberjoerage20-Jul-05 12:44 
GeneralRe: COM error Pin
PizzaAndBeerFitWellTogether27-Apr-07 0:57
MemberPizzaAndBeerFitWellTogether27-Apr-07 0:57 
GeneralRe: COM error Pin
Adam Cheeseman27-Aug-08 6:30
MemberAdam Cheeseman27-Aug-08 6:30 
GeneralRe: COM error Pin
Vadim Tabakman19-Dec-08 22:10
MemberVadim Tabakman19-Dec-08 22:10 
GeneralConsole app Pin
Gavin Jeffrey19-Oct-04 23:21
MemberGavin Jeffrey19-Oct-04 23:21 
GeneralClient side add-in that does the same Pin
urielm24-Feb-04 6:20
Memberurielm24-Feb-04 6: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.

Posted 18 Feb 2004


35 bookmarked