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:
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()
{
const CString defaultPath =
"C:\\Program Files\\Microsoft Office\\Office\\Outlook.exe";
HKEY hKey;
TCHAR szCLSID[255];
TCHAR szPath[255];
DWORD dwBufLen = 255;
LONG lRet;
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;
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;
}