smtp2.shtml
来自「mfc资源大全包含MFC编程各个方面的源码」· SHTML 代码 · 共 148 行
SHTML
148 行
<HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Guy Gascoigne - Piggford"> <TITLE>Internet - SMTP MFC Classes</TITLE></HEAD><body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed"><table WIDTH="100%"><tr WIDTH="100%"><td align=center><!--#exec cgi="/cgi/ads.cgi"--><td></tr></table><CENTER><H3><FONT COLOR="#AOAO99">SMTP MFC Classes</FONT></H3></CENTER><CENTER><H3><HR></H3></CENTER><p>This article was contributed by <a href="mailto:clyburnw@enmu.edu">Wes Clyburn</a></p><html><head><title>SMTP MFC Classes</title></head><body bgcolor="#FFFFFF"><h1>SMTP MFC Classes</h1><p>The classes presented in this article allow your applicationto send e-mail using the SMTP protocol. They are part of alibrary I developed for a series of projects that includes a POP3class, an IMAP class, and a PostOffice class that manages alle-mail activity and provides a storage mechanism (like Outlook'sfolders).</p><p>This implementation of the SMTP protocol differs from theother on this site (which is very good, and easier to understandthan mine) in that I deal with messages as discreet entities thatare passed around to objects that do something with them (likesend 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.</p><p>Additionally, this code shows how to properly construct aheader and cook a message body.</p><p>There are two classes in this package, and I'll describebriefly how they are used:</p><h2>CMessage</h2><p>This class encapsulates an e-mail message. It differentiatesbetween the header and the body, and builds standard header linessuch 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.</p><p>I have a derivative of CMessage that handles file attachments,which I may or may not post later (depending on whether or not Ipost them, which I may or may not, depending).</p><h2>CSMTP</h2><p>This class prepares a CMessage and handles all negotiationwith the server, including getting the local host name for theHELO command, prepping the message header and ensuring that thebody is fit to send (such as the case where user types a singleperiod on a line-- it's part of the message body, but if nothandled correctly, SMTP will treat it as a end-of-data marker andall remaining body text will be lost). I'm sure there are somemonkey wrenches that could be thrown into it that would clobbersome mail readers, and if you find any I will un-ignore you.</p><p>In short, its a relatively robust package that can be used asthe basis for real applications with a little tweaking.</p><h2>Sending Mail</h2><p>Sending a message is easy:</p><pre>void CMyView::OnClickSendButton(){ CMessage msg; CSMTP smtp( "mymailserver.com" ); // You can set server/port later, too.</pre><pre> // 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.";</pre><pre> // 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();} </pre><h2>Using the Package</h2><p>I've included Component-Gallery-ready files in the ZIP thatcan simply be added to your project. You might want to make afolder for them in your SharedIDE directory, and toss their buttsin there. The files are "Mail Message.ogx" and "SMTP.ogx".</p><p>Additionally, a sample project (one of those no-frills,3-minute, to-hell-with-the-user jobbies) that zaps off a messageis included. When extracting, be sure to "Use Folder Names".</p><p>These classes can be included into any MFC project. I've usedthem for ActiveX controls, ISAPI DLLs, and what have you. It'snot inherently threadsafe so I either sprinkle CCrits around whenI need them, or ensure that I only use _local_ CMessage and CSMTPobjects.</p><a href="smtp2_source.zip">Download Project</a><P>Posted on: April 11, 98.<P><HR><TABLE BORDER=0 WIDTH="100%" ><TR><TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD><TD WIDTH="33%"> <CENTER><FONT SIZE=-2>© 1998 Zafir Anjum</FONT> </CENTER></TD><TD WIDTH="34%"><DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV></TD></TR></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?