简单邮件传输协议SMTP封装类_第1页
简单邮件传输协议SMTP封装类_第2页
简单邮件传输协议SMTP封装类_第3页
简单邮件传输协议SMTP封装类_第4页
简单邮件传输协议SMTP封装类_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、简单邮件传输协议smtp封装类在internet ±, emai 1是最流行的传输媒体。这篇文章包括两个协议:.pop 3协议:pop3协议(邮政传输协议)就是指从email服务器接收信件。我已经 捉交了一个封装pop3 i办议的类。该i办议官方的描述你可查阅rfc1225。smtp 协议:smtp (简单邮件传输协议)指发送邮件到它的目的地。有关smtp协 议的细节你可参考rcf 821 .我最新地贡献是封装了 smtp协议。我不能完全实 现smtp协议但你可用它在许多应用场合发送邮件。该类冇若干方法,我选方法 名同smtp命令,所以你很容易理解。在该类中,我再一次使用了 csoc

2、kct作 为类成员,所以我能在线程中使用它下而是该类的编码及注释:/ / smtp.h: interface for the csmtp class./ /#if !defined(afx smtp ii 617f7e82 7f4d 11d1 88a0 00001c302581 includ eddefine afx_smtp_h_617f7e82_7f4d_lldl_88a0_00001c302581_included_ #if _msc_ver >二 1000spragma oncesendif / _msc_ver >= 1000ttdefine connecti0n_che

3、ck 0define hello_check 1#dcfinc mail_ciieck 2define rcpt_check 3define data_start_check 4ttdefine data_exd_check 5define qutt_check 6#dcfinc data_ciieck 7 class csmtppublic:bool ma订();/ cstringgeterrormessage ();bool data(cstring subject, cstring body);cstringgetto ();bool setto(cstring to);cstringg

4、etfrom (); voidsetfrom(cstring from); bool mail (cstring from); bool disconnect (); cstringgethost (); voidsethost(cstring host);bool connect(cstring host, cstring me);bool connect (); csmtp (); virtual csmtp();private:cstringgeterror(cstring response);cstringm_errormcssagc;bool checkresponse (int t

5、ype); intm_noofto;cstringarraymto;cstringmfrom;csockctm_smtpservcr;cstringm_host;; _ttendif/ !defined (afx smtp ii 617f7e82 7f4d 11d1 88a0 00001c302581 include d_)/ smtp.cpp: implementation of the csmtp class/#include "stdafx.hinclude订send. hinclude "smtp. hitifdcf _debugffundef th1s_f1les

6、tatic char this_file=_file_;ttdefine new debug newitendif/ / construction/destruetion / /csmtp: csmtp ()m_noofto = 0;m_smtpserver. create(); _csmtp: "csmtp ()m_smtpserver. close (); _/ connect to the smtp serverbool csmtp:connect()return connect (in host, in_froin); ""/ connect to the

7、 smtp serverbool csmtp:connect(cstringhost, cstring from)if (!m smtpserver.connect (host,25) / 25 for smtp port"m_errormessage = _t("server cannot be connected"); return false;elseif (checkresponse (connect i on_ci1eck) =false) return false;charbuf 512;wsprintf (buf, "helo %srn,

8、(lpcstr) from); m_smtpserver. send(buf, strlen (buf); if (checkresponse(hello_check)二二false) return false;elsereturn true; return true;/ setting the host string void csmtp:sethost(cstring host) mj io st = host;/ returing the host string cstring csmtp:gethost()returnm_host; "/发送quit命令到smtp服务器: b

9、ool csmtp:disconnect()charbuf256;wsprintf (buf, "quit rn,z); m_smtpscrver. send (buf, strlcn (buf); 辻(checkresponse(qu1t_check)=false) return false;elsereturn true;/发送mail命令到smtp服务器: bool csmtp:mail(cstring from) charbuf256;wsprintf (buf, /zmail from:<%s>rnz/, (lpcstr) from); m smtpserver

10、. send (buf, strlen (buf);if (checkresponse (mail_check)二二false)return false;elsereturn true;/ setting the from string void csmtp:setfrom(cstring from) mfrom 二 from; / returing the from string cstring csmtp:getfromo rcturnmfrom; _/ setting the to string bool csmtp:setto(cstring to)charbuf256;m_to. a

11、dd(to); / saving vale of towsprintf (buf, rcpt t0:s>rn, (lpcstr) to); m_smtpscrver. send(buf, strlcn (buf); if (checkresponse (rcpt_check)=false) return false;elsereturn true;/ returing the to stringcstring csmtp:getto()辻(m_to. getsize()>=m_noofto) 一 一m_noofto+; returnm_tom_noofto-l; "&qu

12、ot;elsereturn _t(no more to available/,); "/发送data命令到smtp服务器:bool csmtp:data (cstring subject, cstring body)charbuf256;wsprintf (buf, ,zdatarn,z);m smtpserver. send (buf, strlen (buf);if (checkresponse (data_check)二二false)return false;elsewsprintf (buf, "subject:%srn/z, (lpcstr) subject);

13、m smtpserver. send(buf, strlen (buf);wsprintf (buf, "%srn", (lpcstr) body);m smtpserver send(buf, strlen (buf);wsprintf (buf, rn); m_smtpscrvcr. send(buf, strlen (buf);return true;/本方法检查服务器的响应:bool csmtp:checkresponse(int type)charbuf 1000;char temp3;for (int i=0;i < 512;i+) buf i二'

14、0'/从服务器接收数据m_smtpserver receive (buf, sizeof (buf);strncpy (temp, buf, 3);int temp2 二 atoi (temp);switch (type)case connecti0n_check:if (temp2 != 220)m_errormessage 二 geterror(lpctstr)buf); rcturn false;break;caseiiello_ciieck:if (temp2 != 250)casemerrormessage return false;break;mail_check:if (

15、temp2 !二 250)m_errormcssagc return false;geterror(lpctstr)buf);gcterror(lpctstr)buf);casecasecasecasebreak;rcpt_check:if (tcmp2 != 250)m errormessage = geterror (lpctstr)buf); return false;break;data_start_check:if (temp2 != 354)m_errormessage 二 geterror (lpctstr)buf); return false;break;data_end_ch

16、eck:if (temp2 != 250)m_errormessage = geterror(lpctstr)buf); return false;break;quit_ciieck:if (temp2 != 221)merrormessage 二 geterror(lpctstr)buf); return false;casebreak;data_check:if (temp2 !二 354) m_errormessage = geterror(lpctstr)buf); return false;break; return true;/返回错误消息cstring csmtp:geterro

17、rmessage()returnmerrormessage; "/按照错误号准备错误信息 cstring csmtp:geterror(cstring response) if (response. find("211") return _t("system status or system help reply");i.f (response. find (214) return _t(help message");if (response.find ("220") return _t("service

18、 is ready");if (response. find ("221") return _t("service closing transmission channel");if (response.find ("250") return _t("requested mail action okay, completed'7);if(response. find (251) return _t(z,user not local: will forward to forward path");if(response find (354) return _t("start mail input; end with .);return _t(no error number is matched with “)+rcsponsc;/ just overloading of the mail metho

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论