CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Internet & Networking >> Internet Protocols >> Email


Class for simple MAPI email
Rating:

Rob Manderson (view profile)
April 26, 1999

.

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.
(continued)




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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
BCC - Blind carbon copy can be useful if sending to multiple people - AORD (02/04/2008)
Body Size Limit ???? - Legacy CodeGuru (09/25/2003)
How to send multiple mails within one method? - Legacy CodeGuru (08/24/2003)
MAPI - Legacy CodeGuru (08/20/2003)
How to validate the "Are you sure" dialog - Legacy CodeGuru (08/05/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)