VC++smtp發(fā)送郵件和附件模塊_第1頁
VC++smtp發(fā)送郵件和附件模塊_第2頁
VC++smtp發(fā)送郵件和附件模塊_第3頁
VC++smtp發(fā)送郵件和附件模塊_第4頁
VC++smtp發(fā)送郵件和附件模塊_第5頁
已閱讀5頁,還剩15頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、C+發(fā)送郵件和附件頭文件/*發(fā)送郵件模塊頭文件*可以發(fā)送文本和附件(支持多個附件一起發(fā)送)*/#pragma oncestruct sMaillnfo / 郵件信息char * m_pcUserName; /用戶登錄郵箱的名稱char * m_pcUserPassWord; /用戶登錄郵箱的密碼 char * m_pcSenderName; /用戶發(fā)送時顯示的名稱 char * m_pcSender; /發(fā)送者的郵箱地址 char * m_pcReceiver; /接收者的郵箱地址 char * m_pcTitle; / 郵箱標題char * m_pcBody; /郵件文本正文charchar

2、m_pclPAddr;/服務(wù)器的IPm_pcIPName; /服務(wù)器的名稱(IP與名稱二選一,優(yōu)先取名稱)sMaillnfo()memset(;class CSendMailpublic :CSendMail( void );CSendMail( void );this ,0, sizeof (sMailInfo);public :bool SendMail(sMaillnfo &smaillnfo);/發(fā)送郵件,需要在發(fā)送的時候初始化郵件信void AddFilePath( char * pcFilePath); /添加附件的決定路徑到附件列表中void DeleteFilePath( ch

3、ar * pcFilePath); / 刪除附件路徑,如果有的話void DeleteAllPath( void ); /刪除全部附件的路徑protected :void GetFileName( char * fileName, char * filePath); / 從附件的路徑中獲取文件名稱void Char2Base64( char * pBuff64, char * pSrcBuff, int iLen); / 把 char 類型轉(zhuǎn)換成 Base64類型bool CReateSocket(SOCKET &sock); / 建立 socket 連接bool Logon(SOCKET &

4、sock); /登錄郵箱,主要進行發(fā)郵件前的準備工作intGetFileData(char /*發(fā)送郵件模塊*可以發(fā)送文本和附件(支持多個附件一起發(fā)送) Date:2011-12-01*/#include StdAfx.h#include SendMail.h#include winsock2.h#pragma comment(lib,WSOCK32)CSendMail:CSendMail( void )m_pcFileBuff=NULL;memset(m_cSendBuff,0, sizeof (m_cSendBuff);memset(m_cReceiveBuff,0,sizeof (m_c

5、ReceiveBuff);CSendMail:CSendMail(void )DeleteAllPath();void CSendMail:Char2Base64( char * pBuff64, char * pSrcBuff, int iLen) FilePath);/由文件路徑獲取附件內(nèi)容boolSendHead(SOCKET &sock); /發(fā)送郵件頭boolSendTextBody(SOCKET & sock);/發(fā)送郵件文本正文boolSendFileBody(SOCKET &sock);/發(fā)送郵件附件boolSendEnd(SOCKET & sock);/發(fā)送郵件結(jié)尾prote

6、ctedCList m_pcFilePathList;/ 記錄附件路徑charm_cSendBuff4096;/ 發(fā)送緩沖區(qū)charm_cReceiveBuff1024;char* m_pcFileBuff;/指向附件內(nèi)容sMailInfo m_sMaillnfo;模塊實現(xiàn)文件1 1 1 1 1 1 1 1/ 分配給 pBuff64 f 分配給 pBuff64+1/ point所在的位置static char Base64Encode=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ ; base64 所映射的字符

7、表int point;/每一個源字符拆分的位置,可取2,4,6 ;初始為2point=2;int i;int iIndex;/base64 字符的索引char n=0;/上一個源字符的殘留值for (i=0;iiLen;i+)if (point=2)ilndex=(*pSrcBuff)point)&0x3f;/ 取得 pSrcBuff的高pointelse if (point=4)ilndex=(*pSrcBuff)point)&0xf;/取得pSrcBuff的高pointelse if (point=6)ilndex=(*pSrcBuff)point)&0x3;/取得pSrcBuff的高po

8、intiIndex+=n;/ 與 pSrcBuff-1的低point結(jié)合組成Base64的索引*pBuff64+=Base64Encodeilndex;/n=(*pSrcBuff)(6-point);/確保n的最咼兩位為0由索引表得到pBuff64計算源字符中的殘留值/n=n&0x3f;/源字符的拆分位置上升point+=2;if (point=8) /如果拆分位置為8說明pSrcBuff有6位殘留,可以組成一個完整的Base64字符,所以直接再組合一次ilndex=(*pSrcBuff)&0x3f;/提取低6位,這就是索引了/n=0;point=2;/拆分位置設(shè)為2pSrcBuff+;if

9、(n!=0)*pBuff64+=Base64Encoden;if (iLen%3=2) /如果源字符串長度不是 3的倍數(shù)要用=補全*pBuff64= =;else if (iLen%3=1)*pBuff64+=;*pBuff64= =;void CSendMail:AddFilePath( char * pcFilePath) / 添加附件路徑if (pcFilePath=NULL)return ;int i;char * temp;for (i=0;im_pcFilePathList.GetCount();i+) temp=m_pcFilePathList.GetAt(m_pcFilePat

10、hList.Findlndex(i);if (strcmp(pcFilePath,temp)=0)/如果已經(jīng)存在就不用再添加了return ;m_pcFilePathList.AddTail(pcFilePath);void CSendMail:DeleteFilePath(char * pcFilePath) / 刪除附件路徑int i;char * temp;for (i=0;im_pcFilePathList.GetCount();i+) temp=m_pcFilePathList.GetAt(m_pcFilePathList.Findlndex(i);if (strcmp(temp,p

11、cFilePath)=0)/找到并刪除它,如果沒找到就算了m_pcFilePathList.RemoveAt(m_pcFilePathList.Findlndex(i);delete temp;return ;void CSendMail:DeleteAIIPath(void )m_pcFilePathList.RemoveAII();int CSendMail:GetFileData( char * FilePath)m_pcFileBuff=NULL;if (FilePath=NULL)return 0;CFile f;int len;USES_CONVERSION;if (!f.Open

12、(A2W(FilePath),CFile:modeRead|CFile:modeNoTruncate|CFile:typeBinary)return 0;len=( int )f.GetLength();m_pcFileBuff= new char len+1;memset(m_pcFileBuff,0,len+1);f.Read(m_pcFileBuff,len);f.Close();return len;void CSendMail:GetFileName( char * fileName, char * filePath)if (filePath=NULL | fileName=NULL

13、)return ;int i;for (i=0;ih_addr_list);/使用名稱int ret = connect(sock,(sockaddr* )&servaddr, if (ret = SOCKET_ERROR)return false ;return true ;sizeof (servaddr); / 建立連接bool CSendMail:Logon(SOCKET &sock) recv(sock,m_cReceiveBuff,1024,0);memset(m_cSendBuff,O, sizeof (m_cSendBuff); sprintf_s(m_cSendBuff,HE

14、LO rn);send(sock,m_cSendBuff,strlen(m_cSendBuff),0); recv(sock,m_cReceiveBuff,1024,0);if (m_cReceiveBuff0!= 2| m_cReceiveBuff1!=m_cReceiveBuff2!= 0)return false ;/開始會話5 |memset(m_cSendBuff,0,sprintf_s(m_cSendBuff, sizeof (m_cSendBuff);AUTH LOGINrn);send(sock,m_cSendBuff,strlen(m_cSendBuff),0);recv(s

15、ock,m_cReceiveBuff,1024,0); if (m_cReceiveBuff0!= 3 m_cReceiveBuff2!= 4)return false ;| m_cReceiveBuff1!=/請求登錄3 |memset(m_cSendBuff,0,sizeof (m_cSendBuff);Char2Base64(m_cSendBuff,m_sMaillnfo.m_pcUserName,strlen(m_sMaillnfo.m_pcUserName);rm_cSendBuffstrlen(m_cSendBuff)=m_cSendBuffstrlen(m_cSendBuff)=

16、n;send(sock,m_cSendBuff,strlen(m_cSendBuff),0);/ 發(fā)送用戶名recv(sock,m_cReceiveBuff,1024,0);if (m_cReceiveBuff0!= 3| m_cReceiveBuff1!=3|m_cReceiveBuff2!= 4)return false ;memset(m_cSendBuff,0, sizeof (m_cSendBuff);Char2Base64(m_cSendBuff,m_sMaillnfo.m_pcUserPassWord,strlen(m_sMaillnfo.m_pcUser Password);m

17、_cSendBuffstrlen(m_cSendBuff)=r;m_cSendBuffstrlen(m_cSendBuff)=n;send(sock,m_cSendBuff,strlen(m_cSendBuff),0);/ 發(fā)送用戶密碼recv(sock,m_cReceiveBuff,1024,0);if (m_cReceiveBuff0!= 2| m_cReceiveBuff1!=3|m_cReceiveBuff2!= 5)return false ;return true ; / 登錄成功bool CSendMail:SendHead(SOCKET &sock)int rt;memset(

18、m_cSendBuff,0, sizeof (m_cSendBuff);sprintf_s(m_cSendBuff,MAIL FROM:v%srn ,m_sMaillnfo.m_pcSender);rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);if (rt!=strlen(m_cSendBuff)return false ;recv(sock,m_cReceiveBuff,1024,0);memset(m_cSendBuff,0, sizeof (m_cSendBuff);sprintf_s(m_cSendBuff,RCPT TO:rn ,m_

19、sMaillnfo.m_pcReceiver);rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);if (rt!=strlen(m_cSendBuff)return false ;recv(sock,m_cReceiveBuff,1024,0);memset(m_cSendBuff,O, sizeof (m_cSendBuff);memcpy(m_cSendBuff,DATArn ,strlen( DATArn);rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);if (rt!=strlen(m_cSe

20、ndBuff)return false ;recv(sock,m_cReceiveBuff,1024,0);memset(m_cSendBuff,0,sizeof (m_cSendBuff);sprintf_s(m_cSendBuff,From:%sv%srn,m_sMaillnfo.m_pcSenderName,m_sMaillnfo.m_pcSender);To:INVT.COM.CNv%srn ,m_sSubject:%srnMime-Version:sprintf_s(&m_cSendBuffstrlen(m_cSendBuff),150, Maillnfo.m_pcReceiver)

21、;sprintf_s(&m_cSendBuffstrlen(m_cSendBuff),150, 1.0rnContent-Type: multipart/mixed;boundary=INVTrnrn,m_sMaillnfo.m_pcTitle);rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);if (rt!=strlen(m_cSendBuff)return false ;return truebool CSendMail:SendTextBody(SOCKET &sock)int rt;memset(m_cSendBuff,0, sizeof

22、 (m_cSendBuff); sprintf_s(m_cSendBuff,-INVTrnContent-Type: text/plain;rncharset=gb2312rnrn%srnrn,m_sMaillnfo.m_pcBody);rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);if (rt!=strlen(m_cSendBuff)return falsereturn true ;elsebool CSendMail:SendFileBody(SOCKET &sock)int i;char * filePath;int rt;int len

23、;int pt=O;char fileName128;for (i=O;im_pcFilePathList.GetCount();i+)pt=0;memset(fileName,0,128); filePath=m_pcFilePathList.GetAt(m_pcFilePathList.Findlndex(i); len=GetFileData(filePath);GetFileName(fileName,filePath);sprintf_s(m_cSendBuff,-INVTrnContent-Type:application/octet-stream;rn name=%srnCont

24、ent-Transfer-Encoding: base64rnContent-Disposition: attachment;rnfilename=%srnrn,fileName,fileName);send(sock,m_cSendBuff,strlen(m_cSendBuff),0);while (ptlen)memset(m_cSendBuff,0,sizeof (m_cSendBuff);Char2Base64(m_cSendBuff,&m_pcFileBuffpt,min(len-pt,3000); m_cSendBuffstrlen(m_cSendBuff)=r;m_cSendBuffstrlen(m_cSendBuff)=n;rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);pt+=min(len-pt,3000);if (rt!=strlen(m_cSendBuff)return false ;if (len!=0)delete m_pcFileBuff;return true ;);(smailInfo);bool CSendMail:SendEnd(SOCKET &sock)sprintf_s(m_cSendBuff,-INVT-rn.rnsend(sock,m_cSendBuff,strlen

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論