Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
4.28/5 (16 votes)
18 Feb 2004Public Domain 107.6K   3K   35   14
Using Collaboration Data Objects (CDO) to check for new Exchange email

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

C#
// New session
oSession = new MAPI.Session();
// Will use vEmpty for Empty parameter
Object vEmpty = Missing.Value;
// Logon
oSession.Logon(vEmpty,vEmpty,false,false,vEmpty,vEmpty,vEmpty);
// 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)
    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.

C#
[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";
    proc.Start();}
else
{
    // pull up the existing outlook window
    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.

License

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


Written By
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 0:17
krishnapp5-Aug-11 0:17 
GeneralMy vote of 2 Pin
r.balag1-Jul-10 21:41
r.balag1-Jul-10 21:41 
QuestionHow do I get the contents of the emails? Pin
vincent901529003323-Nov-08 21:21
vincent901529003323-Nov-08 21:21 
GeneralChecking another profile Pin
Adam Cheeseman27-Aug-08 5:36
Adam Cheeseman27-Aug-08 5:36 
GeneralRe: Checking another profile Pin
Adam Cheeseman27-Aug-08 5:38
Adam Cheeseman27-Aug-08 5:38 
never mind i figured it out, you have to set true the new profile field
GeneralOutlook Security??!! Pin
Vasya - dragon20-Apr-06 13:08
Vasya - dragon20-Apr-06 13:08 
GeneralMAPI E LOGON FAILED Pin
MIRo2k27-Aug-05 22:06
MIRo2k27-Aug-05 22:06 
GeneralRe: MAPI E LOGON FAILED Pin
wguerram17-Jan-06 6:35
wguerram17-Jan-06 6:35 
GeneralCOM error Pin
joerage20-Jul-05 11:44
joerage20-Jul-05 11:44 
GeneralRe: COM error Pin
PizzaAndBeerFitWellTogether26-Apr-07 23:57
PizzaAndBeerFitWellTogether26-Apr-07 23:57 
GeneralRe: COM error Pin
Adam Cheeseman27-Aug-08 5:30
Adam Cheeseman27-Aug-08 5:30 
GeneralRe: COM error Pin
Vadim Tabakman19-Dec-08 21:10
Vadim Tabakman19-Dec-08 21:10 
GeneralConsole app Pin
Gavin Jeffrey19-Oct-04 22:21
Gavin Jeffrey19-Oct-04 22:21 
GeneralClient side add-in that does the same Pin
urielm24-Feb-04 5:20
urielm24-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.