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.

Comments
BCC - Blind carbon copy can be useful if sending to multiple people
Posted by AORD on 02/04/2008 09:55pmTo 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!-
ReplyAdd code
Posted by Jatapon on 11/17/2012 03:22pmJat send
ReplyHow to send multiple mails within one method?
Posted by Legacy on 08/24/2003 12:00amOriginally posted by: John
Hi,
how can i send multiple mails within one method?
Replyafter 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();
}
MAPI
Posted by Legacy on 08/20/2003 12:00amOriginally 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
Replysecurity dialog
Posted by Legacy on 07/30/2003 12:00amOriginally 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 ???
ReplyHas anyone information or fixes for this problem ?
Counting sent mails ...
Posted by Legacy on 06/23/2003 12:00amOriginally 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,
ReplyHadi
FormView
Posted by Legacy on 06/18/2003 12:00amOriginally posted by: robert
How can I use the Files (Especially Send Function )
Replyin FormView ,not based on Dialog
Email security dialog
Posted by Legacy on 06/11/2003 12:00amOriginally posted by: Srinivas
ReplyException in Send()
Posted by Legacy on 04/29/2003 12:00amOriginally posted by: Niels Sejersen
-
ReplyContinue
Posted by Jat on 11/17/2012 03:26pmJatt send
ReplySolving Netscape 7.0 problem.
Posted by Legacy on 03/18/2003 12:00amOriginally posted by: straycat
ReplyFixed the problem changing the current directory
Posted by Legacy on 02/28/2003 12:00amOriginally posted by: Fred Liu
ReplyLoading, Please Wait ...