C++实现发送邮件和附件功能
本文实例为大家分享了C++实现发送邮件和附件的具体代码,供大家参考,具体内容如下
头文件
/************************* *发送邮件模块头文件 *可以发送文本和附件(支持多个附件一起发送) **************************/ #pragmaonce structsMailInfo//邮件信息 { char*m_pcUserName;//用户登录邮箱的名称 char*m_pcUserPassWord;//用户登录邮箱的密码 char*m_pcSenderName;//用户发送时显示的名称 char*m_pcSender;//发送者的邮箱地址 char*m_pcReceiver;//接收者的邮箱地址 char*m_pcTitle;//邮箱标题 char*m_pcBody;//邮件文本正文 char*m_pcIPAddr;//服务器的IP char*m_pcIPName;//服务器的名称(IP与名称二选一,优先取名称) sMailInfo(){memset(this,0,sizeof(sMailInfo));} }; classCSendMail { public: CSendMail(void); ~CSendMail(void); public: boolSendMail(sMailInfo&smailInfo);//发送邮件,需要在发送的时候初始化邮件信息 voidAddFilePath(char*pcFilePath);//添加附件的决定路径到附件列表中 voidDeleteFilePath(char*pcFilePath);//删除附件路径,如果有的话 voidDeleteAllPath(void);//删除全部附件的路径 protected: voidGetFileName(char*fileName,char*filePath);//从附件的路径中获取文件名称 voidChar2Base64(char*pBuff64,char*pSrcBuff,intiLen);//把char类型转换成Base64类型 boolCReateSocket(SOCKET&sock);//建立socket连接 boolLogon(SOCKET&sock);//登录邮箱,主要进行发邮件前的准备工作 intGetFileData(char*FilePath);//由文件路径获取附件内容 boolSendHead(SOCKET&sock);//发送邮件头 boolSendTextBody(SOCKET&sock);//发送邮件文本正文 boolSendFileBody(SOCKET&sock);//发送邮件附件 boolSendEnd(SOCKET&sock);//发送邮件结尾 protected: CListm_pcFilePathList;//记录附件路径 charm_cSendBuff[4096];//发送缓冲区 charm_cReceiveBuff[1024]; char*m_pcFileBuff;//指向附件内容 sMailInfom_sMailInfo; };
模块实现文件
/**************************** *发送邮件模块 *可以发送文本和附件(支持多个附件一起发送) *Date:2011-12-01 ******************************/ #include"StdAfx.h" #include"SendMail.h" #include"winsock2.h" #pragmacomment(lib,"WSOCK32") CSendMail::CSendMail(void) { m_pcFileBuff=NULL; memset(m_cSendBuff,0,sizeof(m_cSendBuff)); memset(m_cReceiveBuff,0,sizeof(m_cReceiveBuff)); } CSendMail::~CSendMail(void) { DeleteAllPath(); } voidCSendMail::Char2Base64(char*pBuff64,char*pSrcBuff,intiLen) { //11111111 //分配给pBuff64↑分配给pBuff64+1 //point所在的位置 staticcharBase64Encode[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//base64所映射的字符表 intpoint;//每一个源字符拆分的位置,可取2,4,6;初始为2 point=2; inti; intiIndex;//base64字符的索引 charn=0;//上一个源字符的残留值 for(i=0;i>point)&0x3f;//取得pSrcBuff的高point位 } elseif(point==4) { iIndex=((*pSrcBuff)>>point)&0xf;//取得pSrcBuff的高point位 } elseif(point==6) { iIndex=((*pSrcBuff)>>point)&0x3;//取得pSrcBuff的高point位 } iIndex+=n;//与pSrcBuff-1的低point结合组成Base64的索引 *pBuff64++=Base64Encode[iIndex];//由索引表得到pBuff64 n=((*pSrcBuff)<<(6-point));//计算源字符中的残留值 n=n&0x3f;//确保n的最高两位为0 point+=2;//源字符的拆分位置上升2 if(point==8)//如果拆分位置为8说明pSrcBuff有6位残留,可以组成一个完整的Base64字符,所以直接再组合一次 { iIndex=(*pSrcBuff)&0x3f;//提取低6位,这就是索引了 *pBuff64++=Base64Encode[iIndex];// n=0;//残留值为0 point=2;//拆分位置设为2 } pSrcBuff++; } if(n!=0) { *pBuff64++=Base64Encode[n]; } if(iLen%3==2)//如果源字符串长度不是3的倍数要用'='补全 { *pBuff64='='; } elseif(iLen%3==1) { *pBuff64++='='; *pBuff64='='; } } voidCSendMail::AddFilePath(char*pcFilePath)//添加附件路径 { if(pcFilePath==NULL) { return; } inti; char*temp; for(i=0;i h_addr_list); } intret=connect(sock,(sockaddr*)&servaddr,sizeof(servaddr));//建立连接 if(ret==SOCKET_ERROR) { returnfalse; } returntrue; } boolCSendMail::Logon(SOCKET&sock) { recv(sock,m_cReceiveBuff,1024,0); memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"HELO[]\r\n"); send(sock,m_cSendBuff,strlen(m_cSendBuff),0);//开始会话 recv(sock,m_cReceiveBuff,1024,0); if(m_cReceiveBuff[0]!='2'||m_cReceiveBuff[1]!='5'||m_cReceiveBuff[2]!='0') { returnfalse; } memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"AUTHLOGIN\r\n"); send(sock,m_cSendBuff,strlen(m_cSendBuff),0);//请求登录 recv(sock,m_cReceiveBuff,1024,0); if(m_cReceiveBuff[0]!='3'||m_cReceiveBuff[1]!='3'||m_cReceiveBuff[2]!='4') { returnfalse; } memset(m_cSendBuff,0,sizeof(m_cSendBuff)); Char2Base64(m_cSendBuff,m_sMailInfo.m_pcUserName,strlen(m_sMailInfo.m_pcUserName)); m_cSendBuff[strlen(m_cSendBuff)]='\r'; m_cSendBuff[strlen(m_cSendBuff)]='\n'; send(sock,m_cSendBuff,strlen(m_cSendBuff),0);//发送用户名 recv(sock,m_cReceiveBuff,1024,0); if(m_cReceiveBuff[0]!='3'||m_cReceiveBuff[1]!='3'||m_cReceiveBuff[2]!='4') { returnfalse; } memset(m_cSendBuff,0,sizeof(m_cSendBuff)); Char2Base64(m_cSendBuff,m_sMailInfo.m_pcUserPassWord,strlen(m_sMailInfo.m_pcUserPassWord)); m_cSendBuff[strlen(m_cSendBuff)]='\r'; m_cSendBuff[strlen(m_cSendBuff)]='\n'; send(sock,m_cSendBuff,strlen(m_cSendBuff),0);//发送用户密码 recv(sock,m_cReceiveBuff,1024,0); if(m_cReceiveBuff[0]!='2'||m_cReceiveBuff[1]!='3'||m_cReceiveBuff[2]!='5') { returnfalse; } returntrue;//登录成功 } boolCSendMail::SendHead(SOCKET&sock) { intrt; memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"MAILFROM:<%s>\r\n",m_sMailInfo.m_pcSender); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { returnfalse; } recv(sock,m_cReceiveBuff,1024,0); memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"RCPTTO:<%s>\r\n",m_sMailInfo.m_pcReceiver); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { returnfalse; } recv(sock,m_cReceiveBuff,1024,0); memset(m_cSendBuff,0,sizeof(m_cSendBuff)); memcpy(m_cSendBuff,"DATA\r\n",strlen("DATA\r\n")); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { returnfalse; } recv(sock,m_cReceiveBuff,1024,0); memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"From:\"%s\"<%s>\r\n",m_sMailInfo.m_pcSenderName,m_sMailInfo.m_pcSender); sprintf_s(&m_cSendBuff[strlen(m_cSendBuff)],150,"To:\"INVT.COM.CN\"<%s>\r\n",m_sMailInfo.m_pcReceiver); sprintf_s(&m_cSendBuff[strlen(m_cSendBuff)],150,"Subject:%s\r\nMime-Version:1.0\r\nContent-Type:multipart/mixed;boundary=\"INVT\"\r\n\r\n",m_sMailInfo.m_pcTitle); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { returnfalse; } returntrue; } boolCSendMail::SendTextBody(SOCKET&sock) { intrt; memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"--INVT\r\nContent-Type:text/plain;\r\ncharset=\"gb2312\"\r\n\r\n%s\r\n\r\n",m_sMailInfo.m_pcBody); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { returnfalse; } else { returntrue; } } boolCSendMail::SendFileBody(SOCKET&sock) { inti; char*filePath; intrt; intlen; intpt=0; charfileName[128]; for(i=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。