Click here to Skip to main content
6,291,124 members and growing! (16,119 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: A Public Domain dedication

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

By Mark F Garrison

Using Collaboration Data Objects (CDO) to check for new Exchange email
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:18 Feb 2004
Views:59,214
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
14 votes for this article.
Popularity: 4.23 Rating: 3.69 out of 5
2 votes, 14.3%
1

2
2 votes, 14.3%
3
4 votes, 28.6%
4
6 votes, 42.9%
5

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

// 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.

[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

About the Author

Mark F Garrison


Member
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.
Occupation: Team Leader
Company: PartsSource LLC
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralHow do I get the contents of the emails? Pinmembervincent901529003322:21 23 Nov '08  
GeneralChecking another profile PinmemberAdam Cheeseman6:36 27 Aug '08  
GeneralRe: Checking another profile PinmemberAdam Cheeseman6:38 27 Aug '08  
GeneralOutlook Security??!! PinmemberVasya - dragon14:08 20 Apr '06  
GeneralMAPI E LOGON FAILED PinmemberMIRo2k223:06 7 Aug '05  
GeneralRe: MAPI E LOGON FAILED Pinmemberwguerram7:35 17 Jan '06  
GeneralCOM error Pinmemberjoerage12:44 20 Jul '05  
GeneralRe: COM error PinmemberPartyking0:57 27 Apr '07  
GeneralRe: COM error PinmemberAdam Cheeseman6:30 27 Aug '08  
GeneralRe: COM error PinmemberJubjub22:10 19 Dec '08  
GeneralConsole app PinmembergavinJeffrey23:21 19 Oct '04  
GeneralClient side add-in that does the same Pinmemberurielm6:20 24 Feb '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Feb 2004
Editor: Nishant Sivakumar
Copyright 2004 by Mark F Garrison
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project