Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

Outlook inbox checker

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Apr 20012 min read 90.5K   1.4K   43   10
Sample code on how to use tray icons and check Exchange mailbox.

Sample Image - MailCheck.gif

Introduction

This article is about checking new mail arrival in your Exchange inbox. It is not intended as a "how-to", as the code is not very well designed. You can rather think of it as my contribution to this very good web site. It provides an example of code re-using, and a ready-to-use application for checking mail.

GUI speaks French. You can easily turn it into your language of choice, by editing dialog, menu and strings resources.

What does it do?

The MailCheck application connects to your Exchange server using your default profile. Then it shows a dialog:

Sample Image - MailCheck.gif

telling you whether you have new mail or not. When you close that window, it iconizes itself to the system tray. The icon in the system tray changes when new mail arrives, and it beeps. Once you have restored the dialog window, new mail is considered checked, and the icon changes again. A menu attached to the tray icon offers to run Outlook.

Acknowledgments

Part of the code comes from Microsoft foundation class library: Wakeme by Vajira Weerasekera. It concerns all the MAPI stuff to connect to your Exchange server and check your inbox.

Another part of the code comes from Daniel Zilcsak. You can read his excellent article System Tray Icons - Minimize Your Application To Tray" on this site.

Icons were copied from the excellent POP checker Magic Mail Monitor by Valeriy Ovechkin.

The ugly work

I simply followed Daniel Zilcsak's guidelines on how to use his CTrayIcon class. I created my MFC project, using a dialog-based application. The application's class is CMailCheckApp, and the dialog's class is CMailCheckDlg. I changed CMailCheckDlg to derive from CTrayDialog.

Then I glued Microsoft's sample CWakemeApp, and removed all code that concerns GUI. I added a CMailCheckDlg member variable, named dlg, to CWakemeApp, so that the dialog gets created when the Wakeme app is created. At last, theWakeme CWakemeApp application instance is created in CMailCheckApp::InitInstance instead of the CMailCheckDlg, and theWakeme.dlg.DoModal() is called. That's it ! (I told you it was ugly). Of course, I added the necessary code to handle GUI.

My only real contribution was to code the way to find the path to an Office application (Outlook). Let's have a closer look at this part, which you may find more interesting:

CString CMailCheckDlg::GetOutlookPath()
{
    // In case we don't find anything,
    // let's try at least default installation path
    const CString defaultPath = 
      "C:\\Program Files\\Microsoft Office\\Office\\Outlook.exe";

    HKEY hKey;
    TCHAR szCLSID[255];
    TCHAR szPath[255];
    DWORD dwBufLen = 255;
    LONG lRet;

    // Find Microsoft's CLSID for Outlook.
    // You can also use here Excel.Application, 
    // Word.Application, etc. Whatever you find in the registry.
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        TEXT("Software\\Classes\\Outlook.Application\\CLSID"),
                    0,
                    KEY_QUERY_VALUE,
                    &hKey) != ERROR_SUCCESS) return defaultPath;

    lRet = RegQueryValueEx(hKey,
                    NULL,
                    NULL,
                    NULL,
                    (LPBYTE)szCLSID,
                    &dwBufLen);

    RegCloseKey(hKey);
    if(lRet != ERROR_SUCCESS) return defaultPath;

    dwBufLen = 255;
    // Now read registry for the CLSID we found.
    // Default key under LocalServer32 is
    // installation path.
    CString key = "Software\\Classes\\CLSID\\"+ 
                     CString(szCLSID) +"\\LocalServer32";
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    key,
                    0,
                    KEY_QUERY_VALUE,
                    &hKey) != ERROR_SUCCESS) return defaultPath;

    lRet = RegQueryValueEx(hKey,
                    NULL,
                    NULL,
                    NULL,
                    (LPBYTE)szPath,
                    &dwBufLen);

    RegCloseKey(hKey);
    if(lRet != ERROR_SUCCESS) return defaultPath;

    return szPath;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralProblem with Compile in C++.NET Pin
Sergioc3-Mar-04 1:03
Sergioc3-Mar-04 1:03 
GeneralGetOutlookPath() revisited Pin
Dr. Versaeg13-Jan-04 6:03
Dr. Versaeg13-Jan-04 6:03 
GeneralOutlook/Messaging server communication Pin
Cuong Bang6-Nov-03 17:42
Cuong Bang6-Nov-03 17:42 
GeneralRe: Outlook/Messaging server communication Pin
ricoh ron16-Mar-11 20:05
ricoh ron16-Mar-11 20:05 
GeneralBug Pin
Xavier Dusart21-Oct-03 21:19
Xavier Dusart21-Oct-03 21:19 
GeneralRe: Bug Pin
Led4-Nov-03 3:52
Led4-Nov-03 3:52 
GeneralGreat Code man! Pin
JeffDog12-Dec-02 11:44
JeffDog12-Dec-02 11:44 
GeneralKey Code Pin
Anonymous8-May-04 19:38
Anonymous8-May-04 19:38 
GeneralUpdate Outlook Contacts Database Pin
31-May-02 23:37
suss31-May-02 23:37 
GeneralMAPI Pin
kolmol28-Mar-02 0:04
kolmol28-Mar-02 0:04 

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.