計算機網絡實驗SMTP郵件發(fā)送系統(tǒng)socket編程_第1頁
計算機網絡實驗SMTP郵件發(fā)送系統(tǒng)socket編程_第2頁
計算機網絡實驗SMTP郵件發(fā)送系統(tǒng)socket編程_第3頁
計算機網絡實驗SMTP郵件發(fā)送系統(tǒng)socket編程_第4頁
計算機網絡實驗SMTP郵件發(fā)送系統(tǒng)socket編程_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

實驗四郵件客戶端實現(xiàn)1316-30-何珊珊[實驗名稱]:郵件客戶端實現(xiàn)[實驗器材]:?與因特網連接的計算機網絡系統(tǒng);?主機操作系統(tǒng)為Windows?;?VC6[實驗內容]::要求學生利用已有的套接字常識使用SMTP和POP3的任一種協(xié)議,實現(xiàn)對于特定郵件服務器(mail.163)的發(fā)送郵件功能。[實驗步驟]:在VC6中搭建工程,因為SMTP中用戶名和密碼都要經過64位編碼的處理,所以工程中除了對SMTP的編程外還需要一個64為編碼的轉換函數(shù)。以下是代碼:MailTest.cpp:#include"stdafx.h"#include"SMTP.h"intmain(intargc,char*argv[]){charto[256];chartitle[256];charbody[1024];charstrServer[128];charstrUser[128];charstrPsw[128];charstrSndMail[128];CSMTPsmtp;smtp.Initialize();/*charto[]="heshanshan2512@”;chartitle[]="test";charbody[]="heshanshan09211595”;charstrServer[]="SMTP.163.com”;charstrUser[]="shan_shan2512";charstrPsw[]="shanshan091756”;charstrSndMail[]="shan_shan2512@163.com”;*/ 〃提示輸入的信息printf("strServer:");scanf("%s”,strServer);printf("strSndMail:");scanf("%s”,strSndMail);printf("strUser:");scanf("%s”,strUser);printf("strPsw:");scanf("%s”,strPsw);printf("to:");scanf("%s",to);printf("title:");scanf("%s",title);printf("body:");scanf("%s",body);if(smtp.MailSend(to,title,body,strServer,strUser,strPsw,strSndMail)){printf("successful\n");〃發(fā)送成功}else{printf("fail\n"); 〃發(fā)送失敗}return0;}SMTP.h:#pragmaonce#include<string>#include"Base64.h"#include<Winsock2.h>#pragmacomment(lib,"Ws2_32")#include<windows.h>#include<time.h>usingnamespacestd;classCSMTP{public:CSMTP(void);~CSMTP(void);BOOLInitialize(void);BOOLMailSend(stringto,stringtitle,stringbody,stringstrServer,stringstrUser,stringstrPsw,stringstrSndMail);private:SOCKETm_socket;};SMTP.cpp:220163.comAnti-spamGTforCoremailSystem<163com[20101010]>ehloheshanshan2512250-nail250-PIPELINING250-AUTHLOGINPLAIN250-AUTH=LOGINPLAIN250-corenaillUxp2xKj7kG0xkI17xGrU7I0s8FV2U3Uj8Cz28xlUUUUU7Ic2I0V2Urpbp23UCa0xDrUUUUJ250-STARTTLS2508BITMIMEauthlogin834dXNlcm5hbUU6kGUzaGFuc2hhbjUMTI=834UGFzc3ducmQ6?2hhbnNo¥W4wOTE3NT¥=235Authenticationsuccessfulpailfrom:<heshanshan2512(?163.com>250MailOKreptto:<heshanshan2512(?>250MailOKdata354Enddatawith<CRXLF>.<CRXLF>from:heshanshan2512(?163.comJto:heshanshan2512(?Rsabject:oooIjdfiowjeuih1250MailOKqueuedassmtp3,DdGowKD7H49waIhNAMrAAw—.227S21300785397#include"stdafx.h"#include"SMTP.h"#defineMAX_PACKET_SIZE1024CSMTP::CSMTP(void)〃構造函數(shù){m_socket=NULL;}CSMTP::~CSMTP()//析構函數(shù)BOOLCSMTP::Initialize() //入口函數(shù){WORDwVersionRequested;//用WSAStartup函數(shù)加載套接字庫,并進行版本協(xié)商WSADATAwsaData;interr;wVersionRequested二MAKEWORD(1,1);err二WSAStartup(wVersionRequested,&wsaData);if(err!=0){returnFALSE;}if(LOBYTE(wsaData.wVersion)!二1||HIBYTE(wsaData.wVersion)!=1)WSACleanup();returnFALSE;}returnTRUE;}BOOLCSMTP::MailSend(stringto,stringtitle,stringbody,stringstrServer,stringstrUser,stringstrPsw,stringstrSndMail){sockaddr_inaddrSmtp; 〃配置服務端地址信息constchar*smtpServer=strServer.c_str();constchar*smtpUser二strUser.c_str();constchar*smtpPass二strPsw.c_str();constchar*senderMail=strSndMail.c_str();if(m_socket!=NULL) 〃判斷socket是否可用,若不可用重新創(chuàng)建{closesocket(m_socket);m_socket=NULL;}if(m_socket==NULL){m_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//創(chuàng)建一個客戶端的套接字}structin_addrin;hostent*hosts二gethostbyname(smtpServer);//域名轉換if(hosts!=NULL){memcpy((char**)&(in),hosts->h_addr,hosts->h_length)//把主機地址付給in}else{returnFALSE;}stringaddrc=inet_ntoa(in);〃將網絡字節(jié)序轉換成應用的格式addrSmtp.sin_family=AF_INET;//配置服務端地址信息addrSmtp.sin_addr.s_addr=in.S_un.S_addr;addrSmtp.sin_port=htons(25);//SMTP的端口號為25intret=0;ret二connect(m_socket,(LPSOCKADDR)&addrSmtp,sizeof(addrSmtp));//與服務端進行連接if(ret==SOCKET_ERROR)〃連接失敗{char*err=newchar[1024];sprintf(err,”連接失敗%d",GetLastError());printf("%s\n",err);returnFALSE;}chars[MAX_PACKET_SIZE];charr[MAX_PACKET_SIZE];memset((void*)s,0,MAX_PACKET_SIZE);memset((void*)r,0,MAX_PACKET_SIZE);intlen;stringresponse;//等待服務器回應len=recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len)response=r;intposition二response.substr(0,3).compare("220”);//判斷返回信息的前三個字符是否為220,如果是,表示連接服務器成功if(position==0){len=sprintf((char*)s,"helo%s\r\n",strUser.c_str());//將用戶名賦給s并發(fā)送給服務器len=send(m_socket,s,strlen(s),0);}else{printf(r);if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;?BSlVdUjniej?linN=19>l^os_iu:ft9>|D0S_lU)19>|D0S9S0pfte>|D0S_iu)4!:(」)RU!」d}9S|9(:(0,(S)U9|JlS,S,19>|DOS_lU)pU9S=U9|器若宙盼席聞金粵圍判忌查毋〃?(..u\J\NID01HinV..,sGjeqD))puuds=u9|}(0==uo!i!sod)j!£番朝回呻賓’耆鑿耶’0軟隼顯耆蟾力三W&0WW回旬yffiti親〃:(“os乙)3」ediuo2r(£'o)」isqns?3suodss=uo!i!sodiu!:J=9SU0dS9J}(U響!曾呈麻回旬舞為國間至f//:(O勺力,一_^>0&)\/1/\1'」(¥」9叩)*>|。。,一山)23」或引?BSlVdUjniej}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}//等待登錄回應len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);//接收服務器返回的信息if(len){response=r;intposition二response.substr(0,3).compare("334”);//判斷返回信息的前三個字符是否為334,如果是,表示此時可以輸入64位編碼過的用戶名了if(position==0){unsignedchar*dst=(unsignedchar*)smtpUser;len=strlen((char*)dst);stringstrTmp二Base64::base64_encode(dst,len);〃進行64位編碼len二sprintf((char*)s,"%s\r\n”,(constchar*)strTmp.c_str());len二send(m_socket,s,strlen(s),0);//發(fā)送編碼過的用戶名}else{if(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}〃發(fā)送完用戶名后等待回應len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);//發(fā)送編碼過的密碼,代碼和發(fā)送用戶名時的方式類似if(len){response=r;intposition二response.substr(0,3).compare("334”);if(position==0){unsignedchar*dst=(unsignedchar*)smtpPass;len二strlen((char*)dst);stringstrTmp二Base64::base64_encode(dst,len);len=sprintf((char*)s,"%s\r\n",(constchar*)strTmp.c_str());len=send(m_socket,s,strlen(s),0);}else{if(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}〃發(fā)送完密碼后等待回應看是否登錄成功len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len){response=r;intposition二response.substr(0,3).compare("235”);if(position==0){len=sprintf((char*)s,"MAILFROM:<%s>\r\n",senderMail);len=send(m_socket,s,strlen(s),0); 〃發(fā)送本地郵箱的地址}elseif(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}//header部份len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len){response=r;intposition二response.substr(0,3).compare("250”);if(position==0){len二sprintf((char*)s,"RCPTTO:<%s>\r\n”,to.c_str());//發(fā)送要發(fā)送的郵箱的地址len=send(m_socket,s,strlen(s),0);}else{if(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;//header部份len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len){response=r;intposition二response.substr(0,3).compare("250”);if(position==0){len=sprintf((char*)s,"DATA\r\n");〃發(fā)送data向服務器發(fā)送郵件內容的申請len=send(m_socket,s,strlen(s),0);}else{if(m_socket)closesocket(m_socket);m_socket二NULL;printf(r);returnFALSE;}}elseif(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len){response=r;intposition二response.substr(0,3).compare("354”);if(position==0){chardate_string[MAX_PACKET_SIZE];time_tseconds;〃調用本地時間time(&seconds);strftime(date_string,MAX_PACKET_SIZE,"%a,%d%b%y%H:%M:%S+0800",localtime(&seconds));sprintf((char*)s,"From:%s\r\nTo:%s\r\nDate:%s\r\nSubject:%s\r\n\r\nX-Mailer:%s\r\nX-Priority:%s\r\nMIME-Version:1.0\r\nContent-type:multipart/mixed;boundary=\"%s\"\r\n\r\n",senderMail,to.c_str(),date_string,title.c_str(),"SMailer","3","#BOUNDARY#");len二send(m_socket,s,strlen(s),0);}else{if(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}〃郵件正文sprintf((char*)s,"--%s\r\nContent-Type:%s\r\nContent-Disposition:%s\r\nContent-Transfer-Encoding:%s\r\n\r\n%s\r\n\r\n","#BOUNDARY#","text/plain;charset二gb2312”,"”,"8bit”,body.c_str());len=send(m_socket,s,strlen(s),0);〃郵件結束sprintf((char*)s,"\r\n--%s--\r\n.\r\n","#BOUNDARY#");len=send(m_socket,s,strlen(s),0);len=recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len){response=r;intposition二response.substr(0,3).compare("250”);if(position==0){//退出連接sprintf((char*)s,"QUIT\r\n");len=send(m_socket,s,strlen(s),0);}elseif(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;}//斷開連接len二recv(m_socket,(char*)r,MAX_PACKET_SIZE,0);if(len){response=r;intposition二response.substr(0,3).compare("221”);if(position==0){if(m_socket)closesocket(m_socket);m_socket=NULL;}else{if(m_socket)closesocket(m_socket);m_socket=NULL;printf(r);returnFALSE;}}else{if(m_socket)closesocket(m_socket);m_socket=NULL;returnFALSE;returnTRUE;}Base64.cpp:#include"StdAfx.h"#include"Base64.h"staticconststd::stringbase64_chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ""abcdefghijklmnopqrstuvwxyz""0123456789+/";std::stringBase64::base64_encode(unsignedcharconst*bytes_to_encode,unsignedintin_len){std::stringret;inti=0;intj=0;unsignedcharchar_array_3[3];unsignedcharchar_array_4[4];while(in_len--)(char_array_3[i++]=*(bytes_to_encode++);if(i==3){char_array_4[0]=(char_array_3[0]&0xfc)>>2;char_array_4[1]=((char_array_3[0]&0x03)<<4)+((char_array_3[1]&0xf0)>>4);char_array_4[2]=((char_array_3[1]&0x0f)<<2)+((char_array_3[2]&0xc0)>>6);char_array_4[3]=char_array_3[2]&0x3f;for(i=0;(i<4);i++)ret+=base64_chars[char_array_4[i]];i=0;}}if(i){for(j=i;j<3;j++)char_array_3[j]='0';char_array_4[0]=(char_array_3[0]&0xfc)>>2;char_array_4[1]=((char_array_3[0]&0x03)<<4)+((char_array_3[1]&0xf0)>>4);char_array_4[2]=((char_array_3[1]&0x0f)<<2)+((char_array_3[2]&0xc0)>>6);char_array_4[3]=char_array_3[2]&0x3f;for(j=0;(j<i+1);j++)ret+=base64_chars[char_array_4[j]];while((i++<3))ret+='=';}returnret;}std::stringBase64::base64_decode(std::stringconst&encoded_string){intin_len=encoded_string.size();inti=0;intj=0;intin_=0;unsignedcharchar_array_4[4],char_array_3[3];std::stringret;while(in_len--&& (encoded_string[in_]!= '=') &&is_base64(encoded_string[in_]))(char_array_4[i++]=encoded_string[in_];in_++;if(i==4)(for(i=0;i<4;i++)char_array_4[i]=base64_chars.find(char_array_4[i]);char_array_3[0]=(cha

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論