发送邮件的C语言源代码.doc_第1页
发送邮件的C语言源代码.doc_第2页
发送邮件的C语言源代码.doc_第3页
发送邮件的C语言源代码.doc_第4页
发送邮件的C语言源代码.doc_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

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

文档简介

发送邮件的源代码,模拟smtp的协议写的程序代码:主要有2个文件,一个头文件,一个实现文件头文件如下;文件名:fsSendMail.h文件内容:/-#ifndef fsSendMailH#define fsSendMailH#include #include #include #include #include #include #include #include #include #include #include using namespace std;/-void MakeVector( char *s , vector &v);int ClientSendMail( char *proxyServerName,/使用的代理服务器地址 UINT proxyServerPort, /代理服务器的端口号 const char *proxyUser, /连接代理服务器的用户名 const char * proxyPass, /连接代理服务器的密码 const char * serverHostName, /邮件服务器地址 UINT serverPort, /邮件服务器端口 const char * username, /邮件用户名 const char * pass,/邮件用户对应的密码 const char * senderAddr,/发送邮件的地址 /const char * recptAddress, const char * from, /for most mail server, it only support youself address /发送邮件的地址 const char * to, /接收邮件的地址,每个邮件地址之间用逗号;进行分开 const char * cc, /抄送邮件的地址 const char * bcc, /密送邮件的地址 const char * subject, /邮件的标题 const char * bodytext, /邮件的正文 const char * bodyhtml, /邮件正文的html格式 const char *attachments, /附件内容,各个附件之间采用逗号;进行分开 char *sMsg);/发送邮件的错误应答 #endif实现文件如下:文件名:fsSendMail.cpp文件内容:/-#pragma hdrstop#include fsSendMail.h/-#pragma package(smart_init)#define BUFFER_BLOCK_SIZE 4096#define DEAL_RETURN_CODE(retCode) if(retCode)!=0)return retCode;#define DEAL_SOCK_ERROR(retCode,sock)if(retCode)=SOCKET_ERROR | (retCode)=0)coutSock error: GetLastError()endl;closesocket(sock);return -1;/Auxiliary Functionsint ClientCommandResolve();void InputLine(string & str);int GetResponseCode(SOCKET sock,int correctCode,string& str);BOOL ResponsedCodeFinished(const string& str);u_long ConvertHostnameToLongHostAddress(const char * destAddress);BOOL SendMessage(SOCKET sock, const char* buffer, int bufferLen);/Sub-Functions in Client Commandvoid EncodingBase64(const char* src, char* des);void DecodingBase64(const char* src, char* des);void EncodingBase64Adv(const char* src, int length, char* des);void DecodingBase64Adv(const char* src, int length, char* des);UCHAR SixBitDecodeIndex(char a);BOOL EncodeFileBase64(const string& filename, string& code);BOOL ReadFileToStr(const string& filename, string& code);void HeadTextTemple(const string& command,const string& addr,string & buffer, BOOL bPrintCommond=TRUE);void Date(string& buffer);void From(const string& addr,string & buffer);void To(const vector& to,string & buffer);void Cc(const vector& cc,string & buffer);void Bcc(const vector& bcc,string & buffer);void DataHead(const string& from, const vector& to, const vector& cc, const vector& bcc, const string & subject, const string& bodytext, const string& bodytexthtml, BOOL bHtml, BOOL bAttachment, string& majorSplitTag, string& buffer);void DataBody(BOOL bHtmlBody,BOOL bAttachment,const string& majorSplitTag,const string& bodytext,const string& bodytexthtml,const vector & attachments,string& buffer);void DataBody_PureText(const string& bodytext_base64, string& buffer);void DataBody_TextAndAttachments(const string& bodytext_base64, const vector& attachments, const string& majorSplitTag, string& buffer);void DataBody_HtmlOnly(const string& bodytext_base64, const string& html_base64, const string& majorSplitTag, string& buffer);void DataBody_HtmlAndAttachments(const string& bodytext_base64, const string& html_base64, const vector& attachments, const string& majorSplitTag, const string& subSplitTag, string& buffer);/Agent Application Functionsint SendMail(const string& proxyServerName, UINT proxyServerPort, const string& proxyUser, const string& proxyPass, const string& serverHostName, UINT serverPort, const string& username, const string pass, const string& senderAddr, const vector& recptAddress, const vector& from, /for most mail server, it only support youself address const vector& to, const vector& cc, const vector& bcc, const string & subject, const string & bodytext, const string & bodyhtml, const vector&attachments);int Socks5StartIPv4(const string& proxyServerHostName,u_int proxyServerPort,const string& username, const string& pass,u_long destAddress,u_int destPort,SOCKET& sock);/*int _tmain(int argc, TCHAR* argv, TCHAR* envp)ClientCommandResolve();return 0;*/int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)/ TODO: Place code here./ShowWindow(NULL,SW_HIDE);ClientCommandResolve();return 0;void InputLine(string & str)char ch;str=;while(ch = getchar()!= n)str+=ch;void MakeVector( char *s , vector &v) int i; char *p,*p1; char *sTmp; if( s = NULL) return; if( *s = 0) return; p = (char *)s; while(1) p1 = strchr( p ,;); if( p1 = NULL) v.push_back( p); break; else sTmp = (char *)malloc( p1- p + 2 ); memset( sTmp, 0 , p1- p + 2 ); memcpy( sTmp , p , p1-p); v.push_back( sTmp); free(sTmp ); p=p1; p+; /*参数设置: proxyServerName:代理服务器IP地址 proxyServerPort:代理服务器端口号 proxyUser :连接代理服务器的用户名 proxyPass:连接代理服务器的密码 serverHostName:邮件服务器地址 serverPort:邮件服务器端口 username:连接邮件服务器的用户名 pass:连接邮件服务器的密码 senderAddr:发送邮件的邮件地址 recptAddress: from: 发送邮件的发送者邮件地址 to: cc: bcc: subject:bodytext:bodyhtml: attachments:*/int ClientSendMail( char *proxyServerName, UINT proxyServerPort, const char *proxyUser, const char * proxyPass, const char * serverHostName, UINT serverPort, const char * username, const char * pass, const char * senderAddr, /const char * recptAddress, const char * from, /for most mail server, it only support youself address const char * to, const char * cc, const char * bcc, const char * subject, const char * bodytext, const char * bodyhtml, const char *attachments, char *sMsg) int nRc; int iResult; string s_proxyServerName,s_proxyUser,s_proxyPass,s_serverHostName; string s_username,s_pass,s_senderAddr,s_subject,s_bodytext,s_bodyhtml; s_proxyServerName = proxyServerName; s_proxyUser = proxyUser; s_proxyPass = proxyPass; s_serverHostName =serverHostName; s_username = username; s_pass = pass; s_senderAddr =senderAddr; s_subject = subject; s_bodytext = bodytext; s_bodyhtml = bodyhtml; set stringSet; UINT i; vector recptAddrs; vector fromV,toV,ccV,bccV,attachmentsV; /MakeVector( (char *)recptAddress , recptAddrs); MakeVector( (char *)from , fromV ); MakeVector( (char *)to , toV ); MakeVector( (char *)cc , ccV ); MakeVector( (char *)bcc , bccV ); MakeVector( (char *)attachments , attachmentsV); *sMsg = 0; memset( sMsg , 0 , sizeof( sMsg ) ); WSADATA wsaData; nRc = -1;if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData)!=0)strcpy( sMsg , WSAStartup Error);/return -1; goto L_Err; for(i=0; i toV.size(); i+) stringSet.insert(toVi); for(i=0; i ccV.size(); i+) stringSet.insert(ccVi); for(i=0; i bccV.size(); i+) stringSet.insert(bccVi); for(set:iterator iteSet=stringSet.begin(); iteSet!=stringSet.end(); iteSet+) recptAddrs.push_back(*iteSet); iResult=SendMail(s_proxyServerName,proxyServerPort,s_proxyUser,s_proxyPass,s_serverHostName,serverPort,s_username,s_pass,s_senderAddr,recptAddrs,fromV,toV,ccV, bccV,s_subject,s_bodytext,s_bodyhtml,attachmentsV); if(iResult=-1) /coutUnsuccessfully sent this mail. Save it to file.endl; /unfinished. Here should save the transaction into file. goto L_Err; nRc = 0;L_Err: recptAddrs.clear(); fromV.clear(); toV.clear(); ccV.clear(); bccV.clear(); attachmentsV.clear(); return nRc;int ClientCommandResolve()string serverHostName;UINT serverPort=0;string senderAddr;vector recptAddrs;string username;string pass;string strtemp;vector from;vector to;vector cc;vector bcc;string subject;string bodytext;string bodytexthtml;vectorattachments;/proxystring proxyServerName;UINT proxyServerPort;string proxyUser;string proxyPass;/serverPort=25;/username=;/pass=;/proxyServerName=;/proxyServerPort=;/proxyUser=;/proxyPass=;char paraCommandBUFFER_BLOCK_SIZE;int pos=0;int iResult;string str;string:size_type startPos;WSADATA wsaData;if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData)!=0)cerrsocket init errorendl;return -1;while(1)/system(test.bat);/SMTP Server host nameserverHostName=;/SMPT Server PortserverPort=25;/USER NAMEusername=USER_126126.com;/PASSpass=USER_PAWWORD;/ProxyproxyServerName=;/MAIL FROM/Though rfc822 supports muti-senders, most email server today only support authentication login./So, the fact is one sender one mail.senderAddr=USER_126126.com;from.push_back(senderAddr);/*/RCPT TO/If having multiple receivers, here using spaces to split themif(recptAddrs.empty()/for testingwhile(1)coutRCPT TO:;cout.flush();InputLine(str);while(1)iResult=sscanf(str.c_str(),%s,paraCommand); if(iResult=EOF)break;startPos=str.find(paraCommand,0,strlen(paraCommand);startPos+=strlen(paraCommand);str=str.substr(startPos,str.length()-startPos);recptAddrs.push_back(string(paraCommand);if(recptAddrs.size()=0)coutInput receiver address.endl;continue;break;*/tostrcpy(paraCommand,TO_USER126.com);/to.push_back(paraCommand);/Ccstrcpy(paraCommand,);cc.push_back(paraCommand);/Bccstrcpy(paraCommand,BBC_USER163.com);bcc.push_back(paraCommand);/Subjectsubject=keyboard;/DATA body: textbodytext=hello;/DATA body: html/Most email reader-agents support html coded email. So we support it too.bodytexthtml=;/DATA body: attachmentstrtemp=C:WINDOWSPrierTianKongbak.rar;/附件attachments.push_back(strtemp);/regroup rectpAddres arrayset stringSet;UINT i;for(i=0; i to.size(); i+)stringSet.insert(toi);for(i=0; i cc.size(); i+)stringSet.insert(cci);for(i=0; i bcc.size(); i+)stringSet.insert(bcci);for(set:iterator iteSet=stringSet.begin();iteSet!=stringSet.end(); iteSet+)recptAddrs.push_back(*iteSet);strcpy(paraCommand,TO_USER126.com);recptAddrs.push_back(paraCommand);strcpy(paraCommand,BBC_USER163.com);recptAddrs.push_back(paraCommand);/recptAddrs.push_back();iResult=SendMail(proxyServerName,proxyServerPort,proxyUser,proxyPass,serverHostName,serverPort,username,pass,senderAddr,recptAddrs,from,to,cc, bcc,subject,bodytext,bodytexthtml,attachments);if(iResult=-1)coutUnsuccessfully sent this mail. Save it to file.endl;/unfinished. Here should save the transaction into file.Sleep(60*1000*60);/每1小时发送一次recptAddrs.clear();from.clear();to.clear();cc.clear();bcc.clear();subject=;bodytext=;bodytexthtml=;attachments.clear();return 0;/for multiple response codeint GetResponseCode(SOCKET sock,int correctCode,string& str)int retCode;char bufferBUFFER_BLOCK_SIZE+1;str=;while(1)retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);if(retCode=SOCKET_ERROR | retCode=0)return -1;bufferretCode=0;coutbuffer;cout.flush();str+=buffer;if(ResponsedCodeFinished(str)if(atoi(str.c_str()=correctCode)return 0;elsereturn 1;BOOL ResponsedCodeFinished(const string& str)if(strstr.length()-2!=r | strstr.length()-1!=n)return FALSE;if(str3!=-)return TRUE;char code4;int beginCode=atoi(str.c_str();itoa(beginCode,code,10);std:string:size_type pos=str.rfind(code,str.length()-1);return strpos+3= ;int SendMail(const string& proxyServerName, UINT proxyServerPort, const string& proxyUser, const string& proxyPass, const string& serverHostName, UINT serverPort, const string& username, const string pass, const string& senderAddr, const vector& recptAddress, const vector& from, /for most mail server, it only support youself address const vector& to, const vector& cc, const vector& bcc, const string & subject, const string & bodytext, const string & bodyhtml, const vector&attachments)sockaddr_in serverAddr;SOCKET sock;hostent * host;char bufferBUFFER_BLOCK_SIZE;unsigned int i;BOOL bHtml,bAttachment;string str;if(bodyhtml.length()=0)bHtml=FALSE;elsebHtml=TRUE;if(attachments.size()=0)bAttachment=FALSE;elsebAttachment=TRUE;if(proxyServerName.length()!=0)/using socks5 proxyu_long addr=ConvertHostnameToLongHostAddress(serverHostName.c_str();if(Socks5StartIPv4(proxyServerName,proxyServerPort,proxyUser,proxyPass,addr,serverPort,sock)!=0)cerrProxy server error!endl;elsehost=gethostbyname(serverHostName.c_str();if(host=NULL)coutCannt resolve this host name: GetLastError()h_addr;serverAddr.sin_family=AF_INET;serverAddr.sin_port=htons(serverPort);coutConnecting to inet_ntoa(serverAddr.sin_addr)endl;cout.flush();if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr)=SOCKET_ERROR)coutCannot connect to Server: serverHostName.c_str()endl;cout.flush();closesocket(sock);return -1;/1. welcome wordsDEAL_RETURN_CODE(GetResponseCode(sock,220,str);/2. HELO./We omit HELO, because most Email Server needs authentication./3. EHLOsprintf(buffer,EHLO %srn,senderAddr.c_str();coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,250,str);/4. AUTH LOGIN. /Most authentication takes AUTH LOGIN and AUTH PLAIN. Here we take AUTH LOGIN by default.sprintf(buffer,AUTH LOGINrn);coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,334,str);/2. USER NAME/User name is coded by base64.EncodingBase64(username.c_str(),buffer);strcat(buffer,rn);coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,334,str);/2. password coded by base64EncodingBase64(pass.c_str(),buffer);strcat(buffer,rn);coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,235,str);/2. MAIL FROMsprintf(buffer,MAIL FROM:rn,senderAddr.c_str();coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,250,str);/2. RCPTfor(i=0;irecptAddress.size();i+)sprintf(buffer,RCPT TO: rn,recptAddressi.c_str();coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,250,str);/2. DATAsprintf(buffer,DATArn);coutbuffer;send(sock,buffer,strlen(buffer),0);DEAL_RETURN_CODE(GetResponseCode(sock,354,str);st

温馨提示

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

评论

0/150

提交评论