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:


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


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( "" ); // 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<>";
	msg.AddMultipleRecipients( "Pookie<>;Snuggle Bumpkin<>");
	// Oops, forgot one
	msg.AddRecipient( "", "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.SendMessage( &msg );

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


  • License for commercial use

    Posted by Manoj Prajapat on 10/07/2015 04:55am

    Hi Wes Greetings!!! Myself Manoj Prajapat, I need your email ID for private message to contact you regarding licensing of your code to use in commercial application. Can you please share your email ID. Thanks a lot.

  • Problems with CSMTP in NT-Service Project

    Posted by EgbSoft on 07/25/2006 09:25am

    I've integrated this Class into a NT-Service Project and can send only one E-Mail. I get a runntime-error when I want to send any further E-Mail. The Problem occures at "socket.create". It works without any Problems in a Dialog-Project. Can anybody help me ?

  • Progress Bar?

    Posted by Legacy on 09/15/2003 12:00am

    Originally posted by: Torsten Richter

    Has anybody implemented an Progress Bar for this Class?

  • Attachment

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

    Originally posted by: Ahmed Bilal

    The class is brilliant, but expose the class which handles the attachments.

  • Sending Email using SMTP????

    Posted by Legacy on 11/19/2002 12:00am

    Originally posted by: Praveen Kumar

    Hey all the code does look good..... I haven't tried this one out, yet. Maybe tomorrow. But i have tried out a code availble at It works in a lan, but not on the internet. I get a reply saying cant relay message for :(. Can anybody help me please?????????

  • get_response (CSocket::Receive) error for multi-line response

    Posted by Legacy on 06/04/2002 12:00am

    Originally posted by: Steven K. Lee

    I am having a difficulty and was wondering if you have encountered it as well, when calling "get_response", which calls CSocket::Receive(). I am trying to use Authentication and receive a multiple line response, which is not being handled. Instead, I get the first line only. The next time I call "get_response", instead of getting the reply to my last send, I get the remainder of the first multi-line response. This happens to me when sending the EHLO command. Have you experienced this before? Can you help me out or point me in the right direction? Any help would be greatly appreciated.

    Otherwise, thank you, Wes, for your code contribution of the SMTP classes you placed on CodeGuru. Your source has been very helpful.

  • How can File Attach and need sample example

    Posted by Legacy on 11/20/2001 12:00am

    Originally posted by: Gary Fung

    Dear all,

    I would like to ask how can attach file in Cgi program, I have see those comment below, but i can't find the answer. Would you please give me some example about attach file in CGI program?

    Thanks a lot


  • Does only work in Studio Debug mode (!!)

    Posted by Legacy on 10/30/2001 12:00am

    Originally posted by: Andre

    Good class. But here is a funny thing.
    I can successfully send an e-mail using the debugging mode
    of VS. Executing the binary outside the VS yields a
    "220: SMTP server error" message and nothing is sent.
    Can anybody give me a hint?

  • BUG: CMailMessage::GetRecipient(...) Here is the Fix

    Posted by Legacy on 07/24/2001 12:00am

    Originally posted by: Jorge Rodriguez

    CMailMessage::GetRecipient has a bug when recipient type is CC or BCC.
    These lines are wrong....
    CRecipient to;
    if( nIndex < 0 || nIndex > m_Recipients.GetUpperBound() )
    return FALSE;

    Here is te modified code
    CRecipient to;
    CArray <CRecipient, CRecipient&> *pRecipients = NULL;
    case TO: pRecipients = &m_Recipients; break;
    case CC: pRecipients = &m_CCRecipients; break;
    case BCC: pRecipients = &m_BCCRecipients; break;
    if( nIndex < 0 || nIndex > pRecipients->GetUpperBound() )
    return FALSE;


    Now it's OK!!!

  • Problem with attached Files ....

    Posted by Legacy on 07/09/2001 12:00am

    Originally posted by: Alex

    My problem is, that attached files with blanks like 'This filename includes blanks.txt' are not correct displayed in the mail. It looks like that the ' ' is a seperator .... 

    "This filename includes blanks.txt" -> "This"

    Where in the code can I correct this problem ?

  • Loading, Please Wait ...

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 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date