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.

Updates:

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! 
	 CDialog::OnOK();
} 

Notes:

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



Comments

  • 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;
    		m_message.nRecipCount++;
    		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!

    • Add code

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

      Jat send

      Reply
    Reply
  • How to send multiple mails within one method?

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

    Originally posted by: John

    Hi,

    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)
    {
    mail.Subject(...);
    mail.Attach(...)
    mail.Send();
    }

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

    Reply
  • security dialog

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

    Originally posted by: Roland

    Hello

    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 ?

    Reply
  • Counting sent mails ...

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

    Originally posted by: Hadi Rezaee

    Hi,

    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,
    Hadi

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

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

    Reply
  • Exception in Send()

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

    Originally posted by: Niels Sejersen

    Hi,
    
    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.To("hgm@somewhere.dk");
    NewMail.Subject("Subject text");
    NewMail.Text("Dette er mail teksten");
    NewMail.From("nse@somewhere.dk");
    NewMail.Send();

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

    Any help is appriciated
    Niels

    • Continue

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

      Jatt send

      Reply
    Reply
  • 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 !!

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

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • According to a recent Forrester total economic impact (TEI) study, enterprises can see a significant reduction in total cost of ownership by accessing Oracle Database in the cloud with a pay-as-you-go subscription model. This subscription service gives businesses the ability to scale up application environments for rapid prototyping, with far less time devoted to procuring licenses and deploying IT infrastructure. Read this study to learn how three different companies use Oracle Database in the cloud and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds