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<clyburnw@enmu.edu>"; msg.AddMultipleRecipients( "Pookie<pookie@foo.com>;Snuggle Bumpkin<snuggles@chakka_khan.com>"); // Oops, forgot one msg.AddRecipient( "elvis@graceland.com", "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
Comments
Problems with CSMTP in NT-Service Project
Posted by EgbSoft on 07/25/2006 09:25amI'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 ? EgbSoft@egbsoft.dyndns.org
ReplyProgress Bar?
Posted by Legacy on 09/15/2003 12:00amOriginally posted by: Torsten Richter
Has anybody implemented an Progress Bar for this Class?
ReplyAttachment
Posted by Legacy on 08/26/2003 12:00amOriginally posted by: Ahmed Bilal
The class is brilliant, but expose the class which handles the attachments.
ReplySending Email using SMTP????
Posted by Legacy on 11/19/2002 12:00amOriginally 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 www.codeproject.com. It works in a lan, but not on the internet. I get a reply saying cant relay message for abc@xyz.com :(. Can anybody help me please?????????
Replyget_response (CSocket::Receive) error for multi-line response
Posted by Legacy on 06/04/2002 12:00amOriginally 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.
ReplyHow can File Attach and need sample example
Posted by Legacy on 11/20/2001 12:00amOriginally 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
Gary
ReplyDoes only work in Studio Debug mode (!!)
Posted by Legacy on 10/30/2001 12:00amOriginally posted by: Andre
Good class. But here is a funny thing.
ReplyI 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:00amOriginally posted by: Jorge Rodriguez
ReplyProblem with attached Files ....
Posted by Legacy on 07/09/2001 12:00amOriginally posted by: Alex
Reply3 years now...
Posted by Legacy on 04/16/2001 12:00amOriginally posted by: m_codeMan
Think we could get a peep at your other CMessage class that handles sending attachments? I've tried to implement one from your class and have had mediocre results at best. Please, some insight?
ReplyLoading, Please Wait ...