Environment: VC6 SP4, NT4 SP3, winCE 2.0
Introduction
MailMan is a simple C++ class that makes the task of reading and sending e-mails easy. This class uses the Messaging Application Programming Interface (MAPI) to accomplish its functionalities.
I have seen code listings regarding sending e-mails or reading e-mails in different bits and pieces on this site and on codeproject, but I did not find them as a plug-and-play kind of code. Then I thought of creating a class to encapsulate the entire functionality in one piece.
Requirement
For sending e-mails, you do not need to have Microsoft Outlook (or any other e-mail client) open, but for receiving it you do. (Some have told me you don’t, but experience has shown me otherwise…).
If you happen to be one who disagrees, please e-mail me.
Methods
This class has the following members:
Method | Description |
bool LogonMail() ; | This is the first thing you should do after instantiating the object. |
Int ReadMail(); | This should tell you if there was any UNREAD E-MAIL found in the INBOX folder. If any e-mail was found, the content can be accessed from the mailbox structure of this class. See the code attached |
bool SendMail( CString name, CString address, CString Subject, CString Text); |
This allows you to send e-mails and not only to one but to as many as you would like if the addresses are delimited by semicolons. |
void LogoffMail(); | This is called before you terminate your application. |
Example
First of all, you should have a pointer to CMailMan:
CMailMan *mail=;
This can be defined in the header file of your class.
You then need to allocate it to an instance of this class:
mail = new CMailMan(); // Initialise it if(mail->LogonMail() = false) { // Error }
How to read e-mail:
switch (mail->ReadMail()) { case GOTMAIL: // If you have any UNREAD e-mail in your INBOX, it will be fetched here x.Format(_T("Name:%s Date:%s Address:%s notetext:%s Subject:%s"), mail->mailbox.name, mail->mailbox.date, mail->mailbox.address, mail->mailbox.notetext, mail->mailbox.subject); break; case FAILMAIL: // "Reading Failed – Boy, you got a problem"; break; case NOMAIL: //"No mail... – No worries!"; break; }
How to send e-mail:
mail->SendMail("Saeed","[email protected];[email protected]", "Stop fighting. Make Love, not war.");
How to log off from your e-mail:
//Log off to avoid memory leakage
if(mail)
{
mail->LogoffMail();
delete mail;
}
Piece of cake? You bet.
Demo:
Follow the steps as numbered.
- Enter your e-mail address in the edit control. This is the account that will be used for testing.
- Click the send button a few times (say 2).
- Click the Send/Receive message button of Microsoft Outlook instead of waiting for Outlook to do its checking every few minutes. DO NOT OPEN YOUR E-MAILS in Outlook; the messages will be marked as READ and that is not what we want for this test.
After you have fetched the e-mail messages sent by this application to your INBOX, you can proceed. - Click the Read button – At every click, one UNREAD e-mail is read and displayed. Use the scroll slider to view.
Although I have developed this code on Windows 2000 only, I don’t anticipate any problems to pop up on the NT or XP platforms. However, if you run into any problems, please contact me in a civilized manner with a clear explanation of what the problem is.
Share what you know and ask what you don’t!
// MailMan.h: interface for the CMailMan class.
//
///////////////////////////////////////////////////////////////////need to include mapi headers files mapix,mapiutil,
//mapitags,mapi (.h)#if !defined(AFX_MAILMAN_H__99148C57_15FD_4D06_9D9E_5A66AD50E95A__INCLUDED_)
#define AFX_MAILMAN_H__99148C57_15FD_4D06_9D9E_5A66AD50E95A__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000enum status { OK , CANT_LOGON , CANT_LOAD};
enum readstatus { GOTMAIL,NOMAIL ,FAILMAIL};struct MailBox
{
CString date;
CString name;
CString address;
CString subject;
CString notetext;
int flag;
};class CMailMan
{
public:
CMailMan();
virtual ~CMailMan();int ReadMail();
int LogonMail() ;
void LogoffMail();
bool SendMail( CString name,
CString address,
CString Subject,
CString Text);
MailBox mailbox;protected:
HINSTANCE GetHandle();
HINSTANCE hMAPILib;
public:LHANDLE lhSession;
LPMAPILOGON lpfnMAPILogon;
LPMAPIFINDNEXT lpfnMAPIFindNext;
LPMAPIREADMAIL lpfnMAPIReadMail;
LPMAPILOGOFF lpfnMAPILogoff;
LPMAPIDETAILS lpfnMAPIDetails;
LPMAPIFREEBUFFER lpfnMAPIFreeBuffer;
LPMAPISENDMAIL lpfnMAPISendMail;};
#endif
// !defined(AFX_MAILMAN_H__99148C57_15FD_4D06_9D9E_5A66AD50E95A__INCLUDED_)