CMapi v1.36

Welcome to CMapi, a set of C++ classes to encapsulate sending and receiving 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. 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 etc. To be able to specify different transports, MAPI provides the concept of profiles which are setup using the Mail control panel applet.

 

 

 

Features
Copyright
Usage
History
API Reference
Contacting the Author

 

 

 

Features

 

 

 

Copyright

 

 

 

Usage

 

 

 

History

v1.36 (23 September 2023)

v1.35 (11 May 2023)

v1.34 (19 March 2022)

v1.33 (4 May 2020)

v1.32 (19 March 2020)

v1.31 (29 December 2019)

v1.30 (11 September 2019)

v1.29 (22 April 2019)

v1.28 (14 October 2018)

v1.27 (11 October 2018)

v1.26 (24 November 2015)

v1.25 (22 March 2015)

v1.24 (4 October 2012)

v1.23 (1 April 2011)

v1.22 (6 November 2008)

v1.21 (16 September 2008)

v1.20 (1 January 2007)

v1.19 (22 December 2006)

v1.18 (26 July 2006)

v1.17 (23 July 2006)

v1.16 (19 August 2005)

v1.15 (14 August 2005)

v1.14 (13 August 2005)

v1.13 (30 April 2005)

v1.12 (5 June 2004)

v1.11 (5 May 2004)

22 February 2004

v1.10 (5 February 2004)

v1.09 (22 December 2003)

v1.08 (5 July 2003)

v1.07 (25 January 2003)

2 October 2002

v1.06 (18 July 2000)

v1.05 (22 June 2000)

v1.04 (21 May 2000)

v1.03 (2 April 2000)

v1.02 (28 March 2000)

v1.01 (5 December 1999)

v1.0 (14 May 1999)

 

 

 

API Reference

The API consists of the following 2 classes in the namespace CMapi and their methods and variables:

 

CMessage

CMessage::m_To
CMessage::m_CC
CMessage::m_BCC
CMessage::m_sSubject
CMessage::m_sBody
CMessage::m_Attachments
CMessage::m_AttachmentTitles

CMessage::m_From
CMessage::m_sDateReceived
CMessage::m_sMessageType
CMessage::m_Flags

 

CSession

CSession::CSession
CSession::~CSession
CSession::Logon
CSession::SharedLogon
CSession::LoggedOn
CSession::Logoff
CSession::Send
CSession::Find
CSession::Read
CSession::Delete
CSession::MapiInstalled
CSession::GetLastError

 

 

CMessage::m_To

Remarks

m_To is of type std::vector<CRecipient> 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. "Joe at Work" could map to using an SMTP MAPI transport to send to joe@somecompany.com) or it can be a specific transport address e.g. SMTP:joe@somecompany.com, FAX:34567 etc.

 

CMessage::m_CC

Remarks

m_CC is of type std::vector<CRecipient> 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.

 

CMessage::m_BCC

Remarks

m_BCC is of type std::vector<CRecipient> 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.

 

CMessage::m_sSubject

Remarks

m_sSubject is of type std::string and is the subject line of the email.

 

CMessage::m_sBody

Remarks

m_sBody is of type std::string and is the body of the email.

 

CMessage::m_Attachments

Remarks

m_Attachments is of type std::vector<std::string> and is a list of filenames to be included as attachments in the email.

 

CMessage::m_AttachmentTitles

Remarks

m_AttachmentTitles is of type std::vector<std::string> 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.

 

CMessage::m_From

Remarks

The originator of this email. This is only filled in when the message is returned from the Read function.

 

CMessage::m_sDateReceived

Remarks

A string version of when the emailed was received. This is only filled in when the message is returned from the Read function.

 

CMessage::m_sMessageType

Remarks

The MAPI message type. This is only filled in when the message is returned from the Read function. Corresponds to the MapiMessage::lpszMessageType variable

 

CMessage::m_Flags

Remarks

The MAPI flags for this message. This is only filled in when the message is returned from the Read function. Corresponds to the MapiMessage::flFlags variable

 

 

CSession::CSession

CSession();

Remarks

Standard constructor for the class. This class is the main MAPI support class and contains the functions to actually send the mail message.

 

CSession::~CSession

virtual ~CSession();

Remarks

Standard destructor for the class. Internally this logs you out of MAPI if you're logged in and unloads the MAPI dll.

 

CSession::Logon

bool Logon(const std::string& sProfileName, const std::string& sPassword = _T(""), HWND hParentWnd = nullptr, bool bForceDownload = false);

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

hParentWnd The parent window indicating that if a dialog box is displayed, it is modal with respect to.

bForceDownload true if you want messages to be downloaded before the function returns.

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

 

CSession::SharedLogon

bool Logon(bool bForceDownload = false);

Return Value

true if you were successfully logged in to MAPI otherwise false

Parameters

bForceDownload true if you want messages to downloaded before the function returns.

Remarks

Logons to the MAPI messaging system by using a "shared" MAPI session. i.e. it will try to use an existing mapi session and if there is none, then this function will fail. For further info on "shared" MAPI sessions, please check the MSDN.

 

CSession::LoggedOn

bool LoggedOn() const;

Remarks

Simply accessor which returns true if this instance is logged on to MAPI otherwise false.

 

CSession::Logoff

bool Logon();

Return Value

true if you were successfully logged off from MAPI otherwise false

Remarks

The corollary function to Logon. Internally this function is called in the CSession destructor.

 

CSession::Send

bool Send(CMessage& message, bool bResolve = false, bool bInteractive = false, HWND hParentWnd = nullptr, bool bSendAsHTML = false);

Return Value

true if the message was successfully sent otherwise false.

Parameters

message Message to be sent

bResolve true if the recipient addresses should be resolved otherwise false.

bInteractive true if you want the message to be editable by the end user prior to the message actually being sent.

hParentWnd If bInteractive is true, then this is the parent window to use.

bSendAsHTML Should the body of the email be sent as HTML as opposed to plain text. For more information on this option, please read the release notes for v1.14

Remarks

Sends the message as specified in the "message" parameter, using the MAPI profile currently logged into. If you set bResolve to true, then the recipient's address will be looked up in the profiles address books, prior to sending. This can lead to the following problem: if two people have identical "resolved" names but different e-mail addresses, the CMapi class is unable to send mail to either address because after the address is resolved, the recipient becomes "ambiguous," and MAPISendMail returns the MAPI_E_AMBIGUOUS_RECIPIENT (21) error code. Normally you should accept the default of not to resolve addresses.

 

CSession::Find

bool Find(std::string& sMessageID, const std::string& sSeedMessageID = _T(""), FLAGS flFlags = MAPI_LONG_MSGID, const std::string& sMessageType = _T(""), bool bInteractive = false, HWND hParentWnd = nullptr);

Return Value

true if the message was successfully found otherwise false.

Parameters

sMessageID Upon return, this contains the message ID of the found email.

sSeedMessageID The ID of the message to "seed" the search with.

flFlags The flags to use in the call to MAPIFindNext.

sMessageType The type of message to search for.

bInteractive true if you want any UI to be displayed as part of the search.

hParentWnd If bInteractive is true, then this is the parent window to use.

Remarks

Searches for the specified message. For details on how you search for messages, check out the code in "EnumMessagesDlg.cpp" in the sample app.

 

CSession::Read

bool Read(const std:string& sMessageID, CMessage& message, FLAGS flFlags = MAPI_SUPPRESS_ATTACH, bool bInteractive = false, HWND hParentWnd = nullptr);

Return Value

true if the message was successfully found otherwise false.

Parameters

sMessageID The message ID of the email to read.

message Upon successful return this contains the read message.

flFlags The flags to use in the call to MAPIReadMail.

bInteractive true if you want any UI to be displayed as part of the read.

hParentWnd If bInteractive is true, then this is the parent window to use. If it is left as nullptr, then the window as returned by AfxGetMainWnd() will be used.

Remarks

Reads the specified message. For details on how you read messages, check out the code in "EnumMessagesDlg.cpp" in the sample app.

 

 

CSession::Delete

bool Delete(std::string& sMessageID, bool bInteractive = false, HWND hParentWnd = nullptr);

Return Value

true if the message was successfully deleted otherwise false.

Parameters

sMessageID Upon return, this contains the message ID of the found email.

bInteractive true if you want any UI to be displayed as part of the deletion.

hParentWnd If bInteractive is true, then this is the parent window to use. If it is left as nullptr, then the window as returned by AfxGetMainWnd() will be used.

Remarks

Deletes the specified message.

 

 

CSession::MapiInstalled

bool MapiInstalled() const;

Remarks

Simply accessor which returns true if MAPI is installed and has been correctly initialized ready for this instance to use. The actual loading of the MAPI dll is handled internally by the CSession constructor, meaning it is valid this function anytime after you have constructed a CSession instance.

 

CSession::GetLastError

DWORD GetLastError() const;

Return Value

The last MAPI error generated by this CSession 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 retrieved. MAPI errors are documented in the MAPI.h file in your VC include directory.

 

 

 

Contacting the Author

PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
23 September 2023