Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Outlook inbox checker

0.00/5 (No votes)
17 Apr 2001 1  
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