C++메 일과 첨부 파일 보 내기 기능 구현

본 논문 의 사례 는 C++메 일과 첨부 파일 을 보 내 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
헤더 파일

/*************************
*          
*         (          ) 
**************************/ 
#pragma once 
struct sMailInfo //     
{ 
 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));} 
}; 
class CSendMail 
{ 
public: 
 CSendMail(void); 
 ~CSendMail(void); 
 
public: 
 bool SendMail(sMailInfo &smailInfo);//    ,                
 void AddFilePath(char * pcFilePath);//                
 void DeleteFilePath(char* pcFilePath);//      ,      
 void DeleteAllPath(void);//          
 
protected: 
 void GetFileName(char* fileName,char* filePath);//              
 void Char2Base64(char* pBuff64,char* pSrcBuff,int iLen);// char     Base64   
 bool CReateSocket(SOCKET &sock);//  socket   
 bool Logon(SOCKET &sock);//    ,              
 int GetFileData(char* FilePath);//            
 
 bool SendHead(SOCKET &sock);//      
 bool SendTextBody(SOCKET &sock);//         
 bool SendFileBody(SOCKET &sock);//       
 bool SendEnd(SOCKET &sock);//       
protected: 
 CList<char*,char*> m_pcFilePathList;//       
 
 char m_cSendBuff[4096];//      
 char m_cReceiveBuff[1024]; 
 char* m_pcFileBuff;//       
 sMailInfo m_sMailInfo; 
}; 
모듈 구현 파일

/****************************
*        
*         (          ) 
*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_cReceiveBuff)); 
} 
 
 
CSendMail::~CSendMail(void) 
{ 
 DeleteAllPath(); 
} 
 
 
void CSendMail::Char2Base64(char* pBuff64,char* pSrcBuff,int iLen) 
{ 
 //1 1 1 1 1 1 1 1 
 //    pBuff64 ↑    pBuff64+1 
 //   point      
 static char Base64Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//base64        
 int point;//           ,  2,4,6;   2 
 point=2; 
 int i; 
 int iIndex;//base64      
 char n=0;//           
 for(i=0;i<iLen;i++) 
 { 
  if(point==2) 
  { 
   iIndex=((*pSrcBuff)>>point)&0x3f;//  pSrcBuff  point  
  } 
  else if (point==4) 
  { 
   iIndex=((*pSrcBuff)>>point)&0xf;//  pSrcBuff  point  
  } 
  else if(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='='; 
 } 
 else if(iLen%3==1) 
 { 
  *pBuff64++='='; 
  *pBuff64='='; 
 } 
} 
 
void CSendMail::AddFilePath(char * pcFilePath)//       
{ 
 if(pcFilePath==NULL) 
 { 
  return; 
 } 
 int i; 
 char* temp; 
 for(i=0;i<m_pcFilePathList.GetCount();i++) 
 { 
  temp=m_pcFilePathList.GetAt(m_pcFilePathList.FindIndex(i)); 
  if(strcmp(pcFilePath,temp)==0)//              
  { 
   return; 
  } 
 } 
 m_pcFilePathList.AddTail(pcFilePath); 
} 
 
void CSendMail::DeleteFilePath(char* pcFilePath)//       
{ 
 int i; 
 char* temp; 
 for(i=0;i<m_pcFilePathList.GetCount();i++) 
 { 
  temp=m_pcFilePathList.GetAt(m_pcFilePathList.FindIndex(i)); 
  if(strcmp(temp,pcFilePath)==0)//      ,         
  { 
   m_pcFilePathList.RemoveAt(m_pcFilePathList.FindIndex(i)); 
   delete[] temp; 
   return; 
  } 
 } 
} 
 
 
void CSendMail::DeleteAllPath(void) 
{ 
 m_pcFilePathList.RemoveAll(); 
} 
 
int CSendMail::GetFileData(char* FilePath) 
{ 
 m_pcFileBuff=NULL; 
 if(FilePath==NULL) 
 { 
  return 0; 
 } 
 CFile f; 
 int len; 
  
  
 USES_CONVERSION; 
 if(!f.Open(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) 
 { 
  return; 
 } 
 int i; 
 for(i=0;i<(int)strlen(filePath);i++) 
 { 
  if(filePath[strlen(filePath)-1-i]=='\\') 
  { 
   memcpy(fileName,&filePath[strlen(filePath)-i],i); 
   return; 
  } 
 } 
} 
 
bool CSendMail::CReateSocket(SOCKET &sock) 
{ 
 WORD wVersionRequested; 
 WSADATA wsaData; 
 int err; 
 wVersionRequested = MAKEWORD( 2, 2 ); 
 err = WSAStartup( wVersionRequested, &wsaData ); 
 if ( err != 0 ) 
 { 
  return false; 
 } 
 if ( LOBYTE( wsaData.wVersion ) != 2 || 
  HIBYTE( wsaData.wVersion ) != 2 ) 
 { 
  WSACleanup( ); 
  return false; 
 } 
 sock = socket(AF_INET,SOCK_STREAM,IPPROTO_IP); 
 if (sock == INVALID_SOCKET) 
 { 
  return false; 
 } 
 
 sockaddr_in servaddr; 
 memset(&servaddr,0,sizeof(sockaddr_in)); 
 servaddr.sin_family = AF_INET; 
 servaddr.sin_port = htons(25);//       25   
 if(m_sMailInfo.m_pcIPName=="") 
 { 
  servaddr.sin_addr.s_addr = inet_addr(m_sMailInfo.m_pcIPAddr);//    IP   
 } 
 else 
 { 
  struct hostent *hp=gethostbyname(m_sMailInfo.m_pcIPName);//     
  servaddr.sin_addr.s_addr=*(int*)(*hp->h_addr_list); 
 } 
 
 
 int ret = connect(sock,(sockaddr*)&servaddr,sizeof(servaddr));//     
 if (ret == SOCKET_ERROR) 
 { 
  return false; 
 } 
 
 return true; 
} 
 
 
bool CSendMail::Logon(SOCKET &sock) 
{ 
 recv(sock,m_cReceiveBuff,1024,0); 
 
 memset(m_cSendBuff,0,sizeof(m_cSendBuff)); 
 sprintf_s(m_cSendBuff,"HELO []\r
"); 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') { return false; } memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"AUTH LOGIN\r
"); 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') { return false; } 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)]='
'; 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') { return false; } 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)]='
'; 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') { return false; } return true;// } bool CSendMail::SendHead(SOCKET &sock) { int rt; memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"MAIL FROM:<%s>\r
",m_sMailInfo.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:<%s>\r
",m_sMailInfo.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,0,sizeof(m_cSendBuff)); memcpy(m_cSendBuff,"DATA\r
",strlen("DATA\r
")); 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,"From:\"%s\"<%s>\r
",m_sMailInfo.m_pcSenderName,m_sMailInfo.m_pcSender); sprintf_s(&m_cSendBuff[strlen(m_cSendBuff)],150,"To:\"INVT.COM.CN\"<%s>\r
",m_sMailInfo.m_pcReceiver); sprintf_s(&m_cSendBuff[strlen(m_cSendBuff)],150,"Subject:%s\r
Mime-Version: 1.0\r
Content-Type: multipart/mixed; boundary=\"INVT\"\r
\r
",m_sMailInfo.m_pcTitle); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { return false; } return true; } bool CSendMail::SendTextBody(SOCKET &sock) { int rt; memset(m_cSendBuff,0,sizeof(m_cSendBuff)); sprintf_s(m_cSendBuff,"--INVT\r
Content-Type: text/plain;\r
charset=\"gb2312\"\r
\r
%s\r
\r
",m_sMailInfo.m_pcBody); rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0); if(rt!=strlen(m_cSendBuff)) { return false; } else { return true; } } bool CSendMail::SendFileBody(SOCKET &sock) { int i; char* filePath; int rt; int len; int pt=0; char fileName[128]; for(i=0;i<m_pcFilePathList.GetCount();i++) { pt=0; memset(fileName,0,128); filePath=m_pcFilePathList.GetAt(m_pcFilePathList.FindIndex(i)); len=GetFileData(filePath); GetFileName(fileName,filePath); sprintf_s(m_cSendBuff,"--INVT\r
Content-Type: application/octet-stream;\r
name=\"%s\"\r
Content-Transfer-Encoding: base64\r
Content-Disposition: attachment;\r
filename=\"%s\"\r
\r
",fileName,fileName); send(sock,m_cSendBuff,strlen(m_cSendBuff),0); while (pt<len) { memset(m_cSendBuff,0,sizeof(m_cSendBuff)); Char2Base64(m_cSendBuff,&m_pcFileBuff[pt],min(len-pt,3000)); m_cSendBuff[strlen(m_cSendBuff)]='\r'; m_cSendBuff[strlen(m_cSendBuff)]='
'; 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; } bool CSendMail::SendEnd(SOCKET &sock) { sprintf_s(m_cSendBuff,"--INVT--\r
.\r
"); send(sock,m_cSendBuff,strlen(m_cSendBuff),0); sprintf_s(m_cSendBuff,"QUIT\r
"); send(sock,m_cSendBuff,strlen(m_cSendBuff),0); closesocket(sock); WSACleanup(); return true; } bool CSendMail::SendMail(sMailInfo &smailInfo) { memcpy(&m_sMailInfo,&smailInfo,sizeof(smailInfo)); if(m_sMailInfo.m_pcBody==NULL || m_sMailInfo.m_pcIPAddr==NULL || m_sMailInfo.m_pcIPName==NULL || m_sMailInfo.m_pcReceiver==NULL || m_sMailInfo.m_pcSender==NULL || m_sMailInfo.m_pcSenderName==NULL || m_sMailInfo.m_pcTitle==NULL || m_sMailInfo.m_pcUserName==NULL || m_sMailInfo.m_pcUserPassWord==NULL) { return false; } SOCKET sock; if(!CReateSocket(sock))// { return false; } if(!Logon(sock))// { return false; } if(!SendHead(sock))// { return false; } if(!SendTextBody(sock))// { return false; } if(!SendFileBody(sock))// { return false; } if(!SendEnd(sock))// , sock { return false; } return true; }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기