In this article, you will learn about CMapi, two simple classes to encapsulate sending mail using Simple MAPI.
Introduction
Welcome to CMapi
, two simple classes to encapsulate sending mail using Simple MAPI.
Simple MAPI is a set of functions exported by MAPI32.dll which allows you to send and receive mail in a transport independent way. It is an optional part of all Win32 operating systems since Windows 95 (excluding Windows CE). MAPI is intended more for the corporate environment when compared with the Internet mail standard SMTP, e.g., products such as MS Exchange Server use MAPI extensively. Transport providers are available for a number of messaging transports including Exchange Server, SMTP, Fax, cc:Mail CompuServe, etc. To be able to specify different transports, MAPI provides the concept of profiles which are setup using the Mail control panel applet.
Table of Contents
- Simple and clean C++ interface.
- All the code is UNICODE compliant and build configurations are provided for this. Even though Simple MAPI only exports an ASCII versions of its functions, the class internally performs the necessary conversions.
- The code can be used in a console application or without bringing up any Mapi dialogs if so desired.
- The code gracefully handles the case where MAPI is not installed on client machines. Internally, the class loads the DLL and uses
GetProcAddress()
calls.
- To use the class in your code, simply include cmapi.cpp in your project and
#include cmapi.h
in whichever of your modules needs to make calls to the classes. - Your code will need to include MFC either statically or dynamically.
- You may want to add mapi.h to your pre compiled header to improve compilation speed. A build message will inform you of this.
- To see the class in action, have a look at the code in
InitInstance()
in the module app.cpp.
V1.0 (14th May 1999)
V1.01 (5th December 1999)
- Fixed potential problem where
CMapi
is used in a console app which was giving an ASSERT
- Fixed an assert which can occur if you dismiss the login dialog when doing an interactive MAPI logon
The API consists of the following two classes and their methods and variables:
CMapiMessage
CMapiSession
- CMapiMessage::m_To
- Remarks:
m_To
is of type CStringArray
and contains the array of recipients which the email is to be mailed to. The name of each recipient can be a friendly name (the friendly name is the name which a recipient with an address book entry is known as e.g., "PJ at Work
" could map to using an SMTP MAPI transport to send to pj.naughter@softech-telecom.com) or it can be a specific transport address e.g., SMTP:pjn@indigo.ie, FAX:34567, etc.
- CMapiMessage::m_CC
- Remarks:
m_CC
is of type CStringArray
and contains the array of recipients which the email will be Carbon Copied to. The way addresses are specified is the same as for m_To
.
- CMapiMessage::m_BCC
- Remarks:
m_BCC
is of type CStringArray
and contains the array of recipients which the email will be Blind Carbon Copied to. The way addresses are specified is the same as for m_To
.
- CMapiMessage::m_sSubject
- Remarks:
m_sSubject
is of type CString
and is the subject line of the email.
- CMapiMessage::m_sBody
- Remarks:
m_sBody
is of type CString
and is the body of the email.
- CMapiMessage::m_Attachments
- Remarks:
m_Attachments
is of type CStringArray
and is a list of filenames to be included as attachments in the email.
- CMapiMessage::m_AttachmentTitles
- Remarks:
m_AttachmentTitles
is of type CStringArray
and contains the titles of what each file attachment will be known as to recipients of this message. If you leave this array empty, then the title will be the same as the filename. As an example, have a look at the code in InitInstance()
in app.cpp to see how the autoexec.bat attachment has a title of my autoexec.bat.
- CMapiSession::CMapiSession
- CMapiSession();
Remarks:
Standard constructor for the class. This class is the main MAPI support class and contains the functions to actually send the mail message.
- CMapiSession::~CMapiSession
- ~CMapiSession();
Remarks:
Standard destructor for the class. Internally, this logs you out of MAPI if you're logged in and unloads the MAPI DLL.
- CMapiSession::Logon
- BOOL Logon(const CString& sProfileName, const CString& sPassword = CString(), CWnd* pParentWnd = NULL);
Return Value:
TRUE
if you were successfully logged in to MAPI, otherwise FALSE
.
Parameters:
sProfileName
-- MAPI profile name to use to logon sPassword
-- Password associated with the profile (if any) pParentWnd
-- The parent window indicating that if a dialog box is displayed, it is modal with respect to
Remarks:
Logons to the MAPI messaging system creating a session with it. If you pass an empty profile name, then Logon
will try to interactively logon by presenting the normal MAPI logon dialog. Specifying NULL
as the parent window as is the default will use the window as returned by AfxGetMainWnd()
. Please note that you must be logged on to MAPI prior to sending a message. Internally, the code will ASSERT
to ensure you do not forget to do this.
- CMapiSession::LoggedOn
- BOOL LoggedOn() const;
Remarks:
Simply accessor which returns TRUE
if this instance is logged on to MAPI, otherwise FALSE
.
- CMapiSession::Logoff
- BOOL Logoff();
Return Value:
TRUE
if you were successfully logged of from MAPI, otherwise FALSE
.
Remarks:
The corollary function to Logon
. Internally, this function is called in the CMapiSession
destructor.
- CMapiSession::Send
- BOOL Send(CMapiMessage& message);
Return Value:
TRUE
if the message was successfully sent, otherwise FALSE
.
Parameters:
message
-- Message to be sent
Remarks:
Sends the message as specfied in the "message
" parameter, using the MAPI profile currently logged into.
- CMapiSession::MapiInstalled
- BOOL MapiInstalled() const;
Remarks:
Simply accessor which returns TRUE
if MAPI is installed and has been correctly initialised ready for this instance to use. The actual loading of the MAPI DLL is handled internally by the CMapiSession
constructor, meaning it is valid this function anytime after you have constructed a CMapiSession
instance.
- CMapiSession::GetLastError
- DWORD GetLastError() const;
Return Value:
The last MAPI error generated by this CMapiSession
instance.
Remarks:
Since the class uses MAPI which has its own way of reporting errors different to the standard Win32 way (GetLastError()
), this method allows this value to be retreived. MAPI errors are documented in the MAPI.h file in your VC include directory.
- Package the code up into an OCX, COM Interface or DLL to allow non MFC apps to use the code.
- Provide a better sample app. At the moment, it's very much a test program which tests all of the functions in the classes provided.
- If you have any other suggested improvements, please let me know so that I can incorporate them into the next release.
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.