版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- DB32/T 5306-2025温室气体 产品碳足迹量化方法与要求 车用锂离子电池
- 期中复习模拟检测题(含答案)-2026-2027学年统编版道德与法治八年级上册
- 广西柳州市2025-2026学年高二下学期7月期末质量检测生物试卷(含答案)
- 2025-2026年四川省人教版高一语文上册第3单元阅读理解同步练习题
- 2025-2026年江西省北师大版高二生物第2课生态学练习题
- 2025-2026年物业服务企业设施设备管理与维护考试模拟试卷
- 2026年心理健康教育综合测试题
- T/CI 989-2025玄武岩电缆桥架
- 《人工智能 钢铁大模型数据集技术要求》GBZ 231-2026
- T/CI 1272-2025绿色安全智慧港口 建设总体技术规范
- 2026新学期高三年级立德树人德育实践课件:诚信教育
- 2026年大学摄影(摄影理论基础)试题及答案
- 变形铝及铝合金制品组织检验方法第2部分:低倍组织检验方法
- 2026年贵州省初级注册安全工程师职业资格考试试题及答案安全生产实务·其他
- 人工智能原理及其应用(第5版)课件全套 第1-8章 概述-智能体应用
- 2026年6月浙江高考选考物理试卷试题真题(含答案详解)
- 环水保监理实施细则
- 江苏水利工程设计概(估)算编制规定2017年修订版
- 污水处理PLC控制系统方案设计指南
- 设施设备维护人员面试题及答案
- 1105 非无菌产品微生物限度检查:2020年版 VS 2025年版对比表
评论
0/150
提交评论