Click here to Skip to main content
Licence Public Domain
First Posted 18 Feb 2004
Views 73,715
Downloads 1,975
Bookmarked 34 times

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

By Mark F Garrison | 18 Feb 2004
Using Collaboration Data Objects (CDO) to check for new Exchange email
2 votes, 12.5%
1
1 vote, 6.3%
2
2 votes, 12.5%
3
4 votes, 25.0%
4
7 votes, 43.8%
5
4.28/5 - 16 votes
2 removed
μ 3.68, σa 2.49 [?]

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

Team Leader
PartsSource LLC
United States United States

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioncode to send mail using outlook profile Pinmemberkrishnapp1:17 5 Aug '11  
GeneralMy vote of 2 Pinmemberr.balag22:41 1 Jul '10  
QuestionHow 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    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.

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