簡單郵件傳輸協(xié)議SMTP封裝類_第1頁
簡單郵件傳輸協(xié)議SMTP封裝類_第2頁
簡單郵件傳輸協(xié)議SMTP封裝類_第3頁
簡單郵件傳輸協(xié)議SMTP封裝類_第4頁
簡單郵件傳輸協(xié)議SMTP封裝類_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、簡單郵件傳輸協(xié)議smtp封裝類在internet ±, emai 1是最流行的傳輸媒體。這篇文章包括兩個協(xié)議:.pop 3協(xié)議:pop3協(xié)議(郵政傳輸協(xié)議)就是指從email服務(wù)器接收信件。我已經(jīng) 捉交了一個封裝pop3 i辦議的類。該i辦議官方的描述你可查閱rfc1225。smtp 協(xié)議:smtp (簡單郵件傳輸協(xié)議)指發(fā)送郵件到它的目的地。有關(guān)smtp協(xié) 議的細(xì)節(jié)你可參考rcf 821 .我最新地貢獻(xiàn)是封裝了 smtp協(xié)議。我不能完全實 現(xiàn)smtp協(xié)議但你可用它在許多應(yīng)用場合發(fā)送郵件。該類冇若干方法,我選方法 名同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; "/發(fā)送quit命令到smtp服務(wù)器: 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;/發(fā)送mail命令到smtp服務(wù)器: 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/,); "/發(fā)送data命令到smtp服務(wù)器: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;/本方法檢查服務(wù)器的響應(yīng):bool csmtp:checkresponse(int type)charbuf 1000;char temp3;for (int i=0;i < 512;i+) buf i二'

14、0'/從服務(wù)器接收數(shù)據(jù)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; "/按照錯誤號準(zhǔn)備錯誤信息 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論