SMTP MFC Classes


SMTP MFC Classes

SMTP MFC Classes

The classes presented in this article allow your application
to send e-mail using the SMTP protocol. They are part of a
library I developed for a series of projects that includes a POP3
class, an IMAP class, and a PostOffice class that manages all
e-mail activity and provides a storage mechanism (like Outlook’s
folders).

This implementation of the SMTP protocol differs from the
other on this site (which is very good, and easier to understand
than mine) in that I deal with messages as discreet entities that
are passed around to objects that do something with them (like
send them using SMTP). Additionally, I hide all protocol
"ping-ponging", prefering to do a single SendMessage()
call as opposed to a sequence of "MAIL From:",
"RCPT To:", and "DATA" function calls.

Additionally, this code shows how to properly construct a
header and cook a message body.

There are two classes in this package, and I’ll describe
briefly how they are used:

CMessage

This class encapsulates an e-mail message. It differentiates
between the header and the body, and builds standard header lines
such as From, To, Date, and X-Mailer. In addition, it supports
"friendly" names in the From and To fields. Altogether,
this class allows your application to send "cooked"
messages to the server instead of raw DATA commands.

I have a derivative of CMessage that handles file attachments,
which I may or may not post later (depending on whether or not I
post them, which I may or may not, depending).

CSMTP

This class prepares a CMessage and handles all negotiation
with the server, including getting the local host name for the
HELO command, prepping the message header and ensuring that the
body is fit to send (such as the case where user types a single
period on a line– it’s part of the message body, but if not
handled correctly, SMTP will treat it as a end-of-data marker and
all remaining body text will be lost). I’m sure there are some
monkey wrenches that could be thrown into it that would clobber
some mail readers, and if you find any I will un-ignore you.

In short, its a relatively robust package that can be used as
the basis for real applications with a little tweaking.

Sending Mail

Sending a message is easy:

void CMyView::OnClickSendButton()
{
	CMessage msg;
	CSMTP smtp( "mymailserver.com" ); // You can set server/port later, too.
	// Populate the message
	// Note the friendly names, and that recipients can be added in bulk
	// or individually.
	msg.m_sFrom = "Ultramegahunk<[email protected]>";
	msg.AddMultipleRecipients( "Pookie<[email protected]>;Snuggle Bumpkin<[email protected]_khan.com>");
	// Oops, forgot one
	msg.AddRecipient( "[email protected]", "You're Dead" );
	msg.m_sSubject = "Ouch";
	msg.m_sBody = "Ow, god, mom, the dog he bited me.";
	// Send the message
	// You can connect once, and issue multiple SendMessage()
	// calls before disconnecting-- all messages in an "inbox",
	// for instance.
	smtp.Connect();
	smtp.SendMessage( &msg );
	smtp.Disconnect();
}	

Using the Package

I’ve included Component-Gallery-ready files in the ZIP that
can simply be added to your project. You might want to make a
folder for them in your SharedIDE directory, and toss their butts
in there. The files are “Mail Message.ogx” and “SMTP.ogx”.

Additionally, a sample project (one of those no-frills,
3-minute, to-hell-with-the-user jobbies) that zaps off a message
is included. When extracting, be sure to “Use Folder Names”.

These classes can be included into any MFC project. I’ve used
them for ActiveX controls, ISAPI DLLs, and what have you. It’s
not inherently threadsafe so I either sprinkle CCrits around when
I need them, or ensure that I only use _local_ CMessage and CSMTP
objects.

Download Project

More by Author

Must Read