Header Files
/*********************************************************************** * Send Message module header file * can send text and attachments (support multiple attachments to send together) /#pragma once struct smailinfo//mail message {char* m_ pcusername;//the name of the user logon mailbox char* m_pcuserpassword;//the password for the user's logon mailbox char* m_pcsendername;//the name that is displayed when the user is sent char* The sender's mailbox address char* m_pcreceiver;//recipient's mailbox address char* m_pctitle;//Mailbox Header char* m_pcbody;//mail text body char* m_pcipaddr;//server IP char*
m_pcipname;//the name of the server (IP and name two select one, priority name) Smailinfo () {memset (this,0,sizeof (Smailinfo));};
Class Csendmail {public:csendmail (void);
~csendmail (void); Public:bool SendMail (Smailinfo &smailinfo);//Send mail, you need to initialize mail message void Addfilepath (char * pcfilepath) at the time of sending;
Add the decision path of the attachment to the list of attachments void Deletefilepath (char* pcfilepath);//delete attachment path, if any, void deleteallpath (void);//delete all attachments ' paths protected: void GetFileName (char* filename,char* filePath)//Get file name from path of attachment void Char2base64 (char* pbuff64,char* psrcbuff,int, Ilen )//Convert char type to BASE64 type bool CReatesocket (socket &sock);//Establish Socket connection bool logon (socket &sock);/login mailbox, prepare work for main email before int getfiledata (char* FilePath)//By file path get attachment content bool Sendhead (socket &sock);//Send headers bool Sendtextbody (socket &sock);/Send Message text body bool Se Ndfilebody (socket &sock)//Send message attachment bool Sendend (socket &sock);/Send Message End protected:clist<char*,char*> m_
pcfilepathlist;//record attachment path char m_csendbuff[4096];//send buffer char m_creceivebuff[1024];
char* m_pcfilebuff;//point to attachment content Smailinfo M_smailinfo; };
The
module implements the file
/************************************************************************ * Send mail module * can send text and attachments (support multiple attachments to send) *date : ************************************************************************/#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//Assigned to pbuff64↑ distribution Static char base64encode[for pbuff64+1/Point location = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678
9+/";//base64 map the character chart int point;//The position of each source character split, preferably 2,4,6; initially 2 point=2;
int i; int iindex;//base64 character index char n=0;//last source character's residue for (i=0;i<ilen;i++) {if (point==2) {iindex= (*psrcbuff) >&G
T;point) &0x3f;//obtain Psrcbuff high Point bit} else if (point==4) {iindex= ((*psrcbuff) >>point) &0xf;//obtains the Psrcbuff high point bit} else if (point==6) {iindex= (*psrcbuff) &G T;>point) &0x3;//obtains Psrcbuff's high point bit} iindex+=n;//and PSrcBuff-1 's low point combination to form Base64 index *pbuff64++=base64encode[ The iindex];//is PBuff64 n= ((*psrcbuff) << (6-point)) by the Index table;//calculates the residue value in the source character n=n&0x3f;//ensure that N is up to two bits to 0 point+=2;// The split position of the source character rises 2 if (point==8)//If the split position is 8 indicating that Psrcbuff has 6 bits of residue, can be composed of a complete Base64 character, so just once again {iindex= (*psrcbuff) &0x3f;//
Extract the lower 6 bits, this is the index of the *pbuff64++=base64encode[iindex];//n=0;//residue value is 0 point=2;//split position to 2} psrcbuff++;
} if (n!=0) {*pbuff64++=base64encode[n];
} if (ilen%3==2)//If the source string length is not a multiple of 3 to use ' = ' complement {*pbuff64= ' = ';
else if (ilen%3==1) {*pbuff64++= ' = ';
*pbuff64= ' = ';
} void Csendmail::addfilepath (char * pcfilepath)/Add attachment path {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)//If already exist, do not need to add {return ==0;
} m_pcfilepathlist.addtail (Pcfilepath);
} void Csendmail::D eletefilepath (char* pcfilepath)//delete attachment path {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)//Find and delete it, if not found the {m_pcfilepathlist.removeat (M_pcfilepathlist.findindex (i));
Delete[] temp;
Return
}} void Csendmail::D eleteallpath (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);//outgoing mail is generally 25 port if (m_smailinfo.m_pcipname== "") {servaddr.sin_addr.s_addr = inet_addr (m_s MAILINFO.M_PCIPADDR);//directly using the IP address} else {struct hostent *hp=gethostbyname (m_smailinfo.m_pcipname);//Use name SERVADDR.S In_addr.s_addr=* (int*) (*hp->h_addr_list);
int ret = connect (sock, (sockaddr*) &servaddr,sizeof (SERVADDR));//Establish connection 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\n");
Send (Sock,m_csendbuff,strlen (M_csendbuff), 0);//Start session 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\n");
Send (Sock,m_csendbuff,strlen (M_csendbuff), 0);//Request Login 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)]= ' \ n ';
Send (Sock,m_csendbuff,strlen (M_csendbuff), 0);//sending User name 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)]= ' \ n ';
Send (Sock,m_csendbuff,strlen (M_csendbuff), 0);//Sending User password recv (sock,m_creceivebuff,1024,0);
if (m_creceivebuff[0]!= ' 2 ' | | | m_creceivebuff[1]!= ' 3 ' | | | m_creceivebuff[2]!= ' 5 ') {return false;
Return true;//Login succeeded} bool Csendmail::sendhead (SOCKET &sock) {int rt;
memset (m_csendbuff,0,sizeof (M_csendbuff));
sprintf_s (M_csendbuff, "MAIL from:<%s>\r\n", 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\n", 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\n", strlen ("data\r\n"));
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\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)) {return false;
return true; } boolCsendmail::sendtextbody (SOCKET &sock) {int rt;
memset (m_csendbuff,0,sizeof (M_csendbuff)); sprintf_s (M_csendbuff, "--invt\r\ncontent-type:text/plain;\r\n charset=\" 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)) {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\ncontent-type:application/octet-stream;\r\n name=\"%s\ "\r\" ncontent-transfer-encoding:base64\r\ncontent-disposition:attachment;\r\n filename=\ "%s\" \r\n\r\n ", 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)]= ' \ 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;
BOOL Csendmail::sendend (SOCKET &sock) {sprintf_s (M_csendbuff, "--invt--\r\n.\r\n");
Send (Sock,m_csendbuff,strlen (M_csendbuff), 0);
sprintf_s (M_csendbuff, "quit\r\n");
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_pcRec Eiver==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))//Establish connection {return false; } if (!
Logon (sock))/login mailbox {return false; } if (!
Sendhead (sock))//Send headers {return false; } if (!
Sendtextbody (sock))/Send Message text part {return false; } if (!
Sendfilebody (sock))/Send attachment {return false; } if (!
Sendend (sock))//End message, and close sock {return false;
return true; }