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)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES