Click here to Skip to main content
11,577,643 members (44,563 online)
Click here to Skip to main content

CMapi v1.01 - A MAPI Implementation

, 3 Mar 2000 247.9K 3K 42
Rate this:
Please Sign up or sign in to vote.
An MFC class to encapsulate sending mail using Simple MAPI.
  • Download source files - 12 Kb
  • Introduction

    Welcome to CMapi, 2 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.


    Features
    Usage
    History
    API Reference
    Planned Enhancements
    Contacting the Author


    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 it's 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 which ever 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. An 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 (5 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 2 classes and their methods and variables:

    CMapiMessage:

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


    CMapiSession:

    CMapiSession::CMapiSession
    CMapiSession::~CMapiSession
    CMapiSession::Logon
    CMapiSession::LoggedOn
    CMapiSession::Logoff
    CMapiSession::Send
    CMapiSession::MapiInstalled
    CMapiSession::GetLastError


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


    Contacting the Author

    PJ Naughter
    Email: pjn@indigo.ie
    Web: http://www.naughter.com
    5 December 1999


    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

    Share

    About the Author

    PJ Naughter
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

     
    GeneralMy vote of 4 Pin
    VEMS15-Nov-12 7:32
    memberVEMS15-Nov-12 7:32 
    Questioncan't send an unicode e-mail. Pin
    Rakesh Pahwa5-Dec-08 19:57
    memberRakesh Pahwa5-Dec-08 19:57 
    QuestionCannot send with Outlook 2003 using interactive mode Pin
    pscholl5-Dec-07 5:32
    memberpscholl5-Dec-07 5:32 
    QuestionProfile password? Pin
    CoderCZ25-Jul-07 3:58
    memberCoderCZ25-Jul-07 3:58 
    GeneralRun Cmapi.exe twice Pin
    jvsun11-Jul-06 22:54
    memberjvsun11-Jul-06 22:54 
    Generalmail sending program Pin
    renuchandran21-Feb-06 6:24
    memberrenuchandran21-Feb-06 6:24 
    GeneralRe: mail sending program Pin
    Mihai Moga31-Mar-06 2:10
    memberMihai Moga31-Mar-06 2:10 
    Generalplease help me to take a look this error Pin
    ruppee22-Dec-05 8:29
    memberruppee22-Dec-05 8:29 
    GeneralRe: please help me to take a look this error Pin
    kezhu2-Jan-06 17:32
    memberkezhu2-Jan-06 17:32 
    GeneralSending an email As... Pin
    AlexEvans3-Oct-05 19:48
    memberAlexEvans3-Oct-05 19:48 
    GeneralResource leak Pin
    Dean M Koska2-Oct-04 6:42
    memberDean M Koska2-Oct-04 6:42 
    GeneralOutlook Dialog Size Pin
    jung-kreidler5-Jul-04 23:16
    memberjung-kreidler5-Jul-04 23:16 
    GeneralRe: Outlook Dialog Size Pin
    pjnaughter6-Jul-04 10:23
    memberpjnaughter6-Jul-04 10:23 
    GeneralMessage breaked into small piecess Pin
    imod26-Jun-04 20:47
    memberimod26-Jun-04 20:47 
    GeneralDetermining if a session exists Pin
    Don Kackman24-Jun-04 15:46
    memberDon Kackman24-Jun-04 15:46 
    GeneralRe: Determining if a session exists Pin
    pjnaughter25-Jun-04 0:16
    memberpjnaughter25-Jun-04 0:16 
    GeneralSend html message body with MpaiSendMail Pin
    cristian_boghina12-May-04 0:36
    susscristian_boghina12-May-04 0:36 
    GeneralSend html message body with MpaiSendMail Pin
    cristian_boghina12-May-04 0:36
    membercristian_boghina12-May-04 0:36 
    GeneralRe: Send html message body with MpaiSendMail Pin
    pjnaughter12-May-04 0:54
    memberpjnaughter12-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
    memberpjnaughter10-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
    memberAnthony_Yio9-Feb-04 15:21 
    GeneralRe: MAPI functions Pin
    pjnaughter10-Feb-04 10:10
    memberpjnaughter10-Feb-04 10:10 
    QuestionSuppress Outlook dialog? Pin
    Orxorand15-Jan-04 1:09
    sussOrxorand15-Jan-04 1:09 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.150603.1 | Last Updated 4 Mar 2000
    Article Copyright 2000 by PJ Naughter
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid