Click here to Skip to main content
15,868,340 members
Articles / Desktop Programming / MFC

CMapi v1.01 - A MAPI Implementation

Rate me:
Please Sign up or sign in to vote.
4.82/5 (23 votes)
3 Mar 2000 315.8K   3.3K   43   115
An MFC class to encapsulate sending mail using Simple MAPI
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

Features

  • 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.

Usage

  • 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.

History

V1.0 (14th May 1999)

  • Initial public release

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

API Reference

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.

Planned Enhancements

  • 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.


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

Comments and Discussions

 
GeneralMy vote of 4 Pin
VEMS15-Nov-12 7:32
VEMS15-Nov-12 7:32 
Questioncan't send an unicode e-mail. Pin
Rakesh Pahwa5-Dec-08 19:57
Rakesh Pahwa5-Dec-08 19:57 
QuestionCannot send with Outlook 2003 using interactive mode Pin
pscholl5-Dec-07 5:32
pscholl5-Dec-07 5:32 
QuestionProfile password? Pin
CoderCZ25-Jul-07 3:58
CoderCZ25-Jul-07 3:58 
GeneralRun Cmapi.exe twice Pin
jvsun11-Jul-06 22:54
jvsun11-Jul-06 22:54 
Generalmail sending program Pin
renuchandran21-Feb-06 6:24
renuchandran21-Feb-06 6:24 
GeneralRe: mail sending program Pin
Ștefan-Mihai MOGA31-Mar-06 2:10
professionalȘtefan-Mihai MOGA31-Mar-06 2:10 
Generalplease help me to take a look this error Pin
ruppee22-Dec-05 8:29
ruppee22-Dec-05 8:29 
GeneralRe: please help me to take a look this error Pin
kezhu2-Jan-06 17:32
kezhu2-Jan-06 17:32 
GeneralSending an email As... Pin
AlexEvans3-Oct-05 19:48
AlexEvans3-Oct-05 19:48 
GeneralResource leak Pin
Dean M Koska2-Oct-04 6:42
Dean M Koska2-Oct-04 6:42 
GeneralOutlook Dialog Size Pin
jung-kreidler5-Jul-04 23:16
jung-kreidler5-Jul-04 23:16 
GeneralRe: Outlook Dialog Size Pin
pjnaughter6-Jul-04 10:23
pjnaughter6-Jul-04 10:23 
GeneralMessage breaked into small piecess Pin
imod26-Jun-04 20:47
imod26-Jun-04 20:47 
sometimes a big message is breaked apart into small pieces due to server limitation of maximum size of message. how can i merge it back after downloading? any idea?

GeneralDetermining if a session exists Pin
Don Kackman24-Jun-04 15:46
Don Kackman24-Jun-04 15:46 
GeneralRe: Determining if a session exists Pin
pjnaughter25-Jun-04 0:16
pjnaughter25-Jun-04 0:16 
GeneralSend html message body with MpaiSendMail Pin
Member 10339912-May-04 0:36
Member 10339912-May-04 0:36 
GeneralSend html message body with MpaiSendMail Pin
Member 10339912-May-04 0:36
Member 10339912-May-04 0:36 
GeneralRe: Send html message body with MpaiSendMail Pin
pjnaughter12-May-04 0:54
pjnaughter12-May-04 0:54 
GeneralUpdating the Outlook Address Book through MAPi calls Pin
f Goines10-Feb-04 12:06
sussf Goines10-Feb-04 12:06 
GeneralRe: Updating the Outlook Address Book through MAPi calls Pin
pjnaughter10-Feb-04 22:59
pjnaughter10-Feb-04 22:59 
GeneralRe: Updating the Outlook Address Book through MAPi calls Pin
f goines11-Feb-04 3:43
sussf goines11-Feb-04 3:43 
GeneralMAPI functions Pin
Anthony_Yio9-Feb-04 15:21
Anthony_Yio9-Feb-04 15:21 
GeneralRe: MAPI functions Pin
pjnaughter10-Feb-04 10:10
pjnaughter10-Feb-04 10:10 
QuestionSuppress Outlook dialog? Pin
Holli H.15-Jan-04 1:09
Holli H.15-Jan-04 1:09 

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.