Class for simple MAPI email


Application Security Testing: An Integral Part of DevOps


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("UltraMaroon@email.msn.com");				//  Set recipient name
	 mail.To("someoneelse@somewhereelse.com");			//  Second recipient
	 mail.Cc("cc@cc.com");						//  CC recipient
	 mail.From("user@somewhere.com"); 				//  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("someone@ihug.co.nz");
    Note: while testing if you BCC yourself twice you only recieve one message!

  • 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

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

  • You must have javascript enabled in order to post comments.

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.