Class for simple MAPI email


I needed the ability to create and send email from within my application (it's an online trivia game called MindProbe). After messing around with the various OCX based solutions available I settled on a cut and paste of the code embedded deep within CDocument to access MAPI. I changed things a lot and abstracted it into it's own class. This class handles multiple attached files as well as multiple recipients and multiple cc recipients.

This class only handles sending email - that's all I needed at this stage. Usage couldn't be easier.


This update fixes the problems noted with multiple recipients. It now copies the strings you pass rather than simply saving pointers to them.

I've also fixed the library reference count problem mentioned by one commentator.

#include <mapi.h>
#include "imapi.h"

void CBugReport::OnOK() 
	 CIMapi  mail;
	 mail.To("");				//  Set recipient name
	 mail.To("");			//  Second recipient
	 mail.Cc("");						//  CC recipient
	 mail.From(""); 				//  Identify sender (not strictly necessary since MAPI will fill this in for you)
	 mail.Subject("Test Email");					//  Subject of this email
	 mail.Attach("somefilename");					//  Attaching a file
	 mail.Attach("someotherfile", "different_name_for_recipient");  //  Attach another file but give it a different name inside the email itself 
	 // Put text of message in body
	 mail.Text("Body text for this email"); 			//  Set body text
	 mail.Send();							//  Now send the mail! 


I used calloc(), realloc() and free() for memory allocation for the recipient and attachment lists. This is primarily because the corresponding members in the MAPI structures are pointers to arrays of structures and it didn't make much sense to manipulate separate data structures simply to be 'pure' and stay within the confines of new and delete. In addition, I rely on the Microsoft C runtime library behaviour with regard to realloc() and free(). The library allows NULL pointers without causing errors - in the case of free() it simply returns; in the case of realloc() it treats a realloc() with a NULL pointer as though it was a calloc(). If your run time library behaves differently you may need to examine the code in the CIMapi destructor.

You need to have a default simple MAPI client defined on your system for this class to work. I use Outlook Express exclusively for my email and have it set as the default simple MAPI client.

Download source


  • BCC - Blind carbon copy can be useful if sending to multiple people

    Posted by AORD on 02/04/2008 09:55pm

    To use BCC (Blind carbon copy),
    Add to the IMapi.h file:
    BOOL			BCc(LPCTSTR recip);
    Add to the IMapi.cpp file:
    BOOL CIMapi::BCc(LPCTSTR recip)
    	if (AllocNewTo())
    		//	We succeeded in allocating a new recipient record
    		m_message.lpRecips[m_message.nRecipCount].lpszName = (LPTSTR) malloc(strlen(recip) + 1);
    		strcpy(m_message.lpRecips[m_message.nRecipCount].lpszName, recip);
    		m_message.lpRecips[m_message.nRecipCount].ulRecipClass = MAPI_BCC;
    		return TRUE;
    	m_error = IMAPI_FAILCC;
    	return FALSE;
    To add a BCC recipent use: mail.BCC("");
    Note: while testing if you BCC yourself twice you only recieve one message!

    • Add code

      Posted by Jatapon on 11/17/2012 03:22pm

      Jat send

  • How to send multiple mails within one method?

    Posted by Legacy on 08/24/2003 12:00am

    Originally posted by: John


    how can i send multiple mails within one method?
    after the first mail.send() call the program crashes(Debug Assertion failed).
    I basically created a loop which executes the following sequence:
    CIMapi mail;
    mail.To(...); //always the same recipient
    while (condition)

  • MAPI

    Posted by Legacy on 08/20/2003 12:00am

    Originally posted by: Sara Griffin

    What is Mapi? When I go to email my resume from Quick and Easy Resume it brings up that I do not MAPI to email?

    Thank You

  • security dialog

    Posted by Legacy on 07/30/2003 12:00am

    Originally posted by: Roland


    I found another problem with the outlook security dialog (another application try to send ...) while sending a mail. The security dialog has three buttons. Yes, No and help. If I press the help button my application crashes with stack overflow failure in the clbcatq.dll ???
    Has anyone information or fixes for this problem ?

  • Counting sent mails ...

    Posted by Legacy on 06/23/2003 12:00am

    Originally posted by: Hadi Rezaee


    First, i want to thanks you for this nice mapi engine :)
    But i had a question in my mind, how can i get the sent mails ?

    For instance, i add 350 mail addres as recieption.
    And then i send a mail to all of them, how can i get the counter ?

    Best Regards,

  • FormView

    Posted by Legacy on 06/18/2003 12:00am

    Originally posted by: robert

    How can I use the Files (Especially Send Function )
    in FormView ,not based on Dialog

  • Email security dialog

    Posted by Legacy on 06/11/2003 12:00am

    Originally posted by: Srinivas

    while sending an automatic Email from the application I get an Dialog box which says 
    "some program is trying to send an E-Mail on behalf of you"
    how to avoid this security through application?

    I am trying to develop a COM add-ins using VB to avoid this dialog box.Please send me necessary code.

    Thanks In advance.

  • Exception in Send()

    Posted by Legacy on 04/29/2003 12:00am

    Originally posted by: Niels Sejersen

    This was just what I was looking for. Unfortunately I get two:
    First-chance exception in TestProgram.exe (KERNEL32.DLL): 0x000006BA: (no name).

    They come when doing:
    int nError = m_lpfnSendMail(0, (ULONG) pParentWnd->GetSafeHwnd(), &m_message, MAPI_LOGON_UI | flags, 0);

    The nError is 0: SUCCESS_SUCCESS, and the mail is also received correctly at the reciepient.
    But still, I would like to get rid of the exceptions

    Here is what I do:
    CIMapi NewMail;
    NewMail.Subject("Subject text");
    NewMail.Text("Dette er mail teksten");

    I am using VC++ 6, Outlook 97, on an NT4 machine.

    Any help is appriciated

    • Continue

      Posted by Jat on 11/17/2012 03:26pm

      Jatt send

  • Solving Netscape 7.0 problem.

    Posted by Legacy on 03/18/2003 12:00am

    Originally posted by: straycat

    I try to use this class to call my Netscape Messenger , but this don't work as should be . To fix this I change the following line :

    int nError = m_lpfnSendMail(0, (ULONG) pParentWnd->GetSafeHwnd(), &m_message, MAPI_DIALOG | MAPI_LOGON_UI | flags, 0);

    I just added the flag MAPI_DILAOG and everithing works fine !!

  • How to remove the Send Dialog box

    Posted by Legacy on 02/28/2003 12:00am

    Originally posted by: Raju

    while sending an automatic Email from the application I get an Dialog box which says
    "some program is trying to send an E-Mail on behalf of you"

    how to avoid this security through application.

    thanks in advance.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date