使用VSMFC高手帮忙找一个SMTP封装类发送邮件用最好支持身份验证.docx
《使用VSMFC高手帮忙找一个SMTP封装类发送邮件用最好支持身份验证.docx》由会员分享,可在线阅读,更多相关《使用VSMFC高手帮忙找一个SMTP封装类发送邮件用最好支持身份验证.docx(18页珍藏版)》请在冰豆网上搜索。
![使用VSMFC高手帮忙找一个SMTP封装类发送邮件用最好支持身份验证.docx](https://file1.bdocx.com/fileroot1/2023-5/17/24da1ab0-d1fb-4066-bfdb-a094725b5e3a/24da1ab0-d1fb-4066-bfdb-a094725b5e3a1.gif)
使用VSMFC高手帮忙找一个SMTP封装类发送邮件用最好支持身份验证
intCMail:
:
sendmail()
{
intcrecv;
if(smtplogin()==0)
{
return0;
}
//发送mailfrom返回250
sprintf(m_sendbuff,"%s%s%s","mailfrom:
<",m_user,"@>\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"250",3)!
=NULL)
{
m_error=MAIL_FROM_ERROR;
return0;
}
//发送rcptto返回250
sprintf(m_sendbuff,"%s%s%s","rcptto:
<",m_rcptto,">\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"250",3)!
=NULL)
{
m_error=RCPT_TO_ERROR;
return0;
}
//发送data\r\n返回354
sprintf(m_sendbuff,"%s","data\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"354",3)!
=NULL)
{
m_error=DATA_ERROR;
return0;
}
//信头
if(!
SetMailHeader(m_sendbuff))
{
m_error=MAILHEADER_ERROR;
return0;
}
if(senddata()==0)
{
return0;
}
//发送空行
sprintf(m_sendbuff,"%s","\r\n\r\n");
if(senddata()==0)
{
return0;
}
//发送邮件body
sprintf(m_sendbuff,"%s%s",m_data,"\r\n");
if(senddata()==0)
{
return0;
}
if(m_attachment!
=NULL)
{
char**pp;
for(pp=m_attachment;*pp!
=0;pp++)
{
FILE*fp;
char*buf;
//准备发送附件,首先发送分界线,必须!
sprintf(m_sendbuff,"%s%s%s","--",BOUNDARY_TEXT,"\r\n");
if(senddata()==0)
{
return0;
}
sprintf(m_sendbuff,"%s%s%s","Content-Type:
application/x-msdownload;name=\"",strrchr(*pp,'\\')+1,"\"\r\n");
if(senddata()==0)
{
return0;
}
sprintf(m_sendbuff,"%s","Content-Transfer-Encoding:
base64\r\n");
if(senddata()==0)
{
return0;
}
sprintf(m_sendbuff,"%s%s%s","Content-Disposition:
attachment;filename=\"",strrchr(*pp,'\\')+1,"\"\r\n\r\n");
if(senddata()==0)
{
return0;
}
//将文件base64编码后发送
fp=fopen(*pp,"rb");
if(fp==NULL)
{
m_error=OPEN_FILE_ERROR;
return0;
}
buf=newchar[BUFFERSIZE+1];
memset(buf,0,BUFFERSIZE+1);
intreadcount=0;
while(!
feof(fp))
{
readcount=fread(buf,1,BUFFERSIZE,fp);
if(readcount>0)
{
*(buf+readcount)='\0';
sprintf(m_sendbuff,"%s%s",base64(buf,ENCODE,readcount),"\r\n");
if(senddata()==0)
{
return0;
}
memset(buf,0,BUFFERSIZE+1);
}
}
sprintf(m_sendbuff,"%s","\r\n");
if(senddata()==0)
{
return0;
}
delete[]buf;
fclose(fp);
}
}
if(m_attachment!
=NULL)
{
//附件发送完毕,发送分界线
sprintf(m_sendbuff,"%s%s%s","--",BOUNDARY_TEXT,"--\r\n");
if(senddata()==0)
{
return0;
}
}
//发送.\r\n结束返回250
sprintf(m_sendbuff,"%s",".\r\n");
if(senddata()==0)
{
return0;
}
crecv=recv(s,m_recvbuff,MAX_LENGTH,0);
if(strncmp(m_recvbuff,"250",3)!
=NULL)
{
m_error=END_DATA_ERROR;
return0;
}
else
{
showmessage(NULL,"邮件发送成功","",NULL);
}
//退出
sprintf(m_sendbuff,"%s","quit\r\n");
if(senddata()==0)
{
return0;
}
if(closesocket(s)!
=0)
{
m_error=CLOSE_SOCKET_ERROR;
return0;
}
if(WSACleanup()!
=0)
{
m_error=CLEANUP_WSA_ERROR;
return0;
}
return1;
}
intCMail:
:
smtplogin()
{
//调用成员函数connect
if(connect()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strstr(m_recvbuff,"220")==NULL)//220
{
m_error=UNKNOWEN_ERROR;
return0;
}
//发送helo返回250
sprintf(m_sendbuff,"%s","helo543880135\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"250",3)!
=NULL)
{
m_error=HELO_ERROR;
return0;
}
//发送authlogin返回334
sprintf(m_sendbuff,"%s","authlogin\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"334",3)!
=NULL)
{
m_error=AUTH_LOGIN_ERROR;
return0;
}
//发送经过base64编码后的用户名,返回334
sprintf(m_sendbuff,"%s%s",base64(m_user,ENCODE,strlen(m_user)),"\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"334",3)!
=NULL)
{
m_error=USER_ERROR;
return0;
}
//发送经过base64编码后的密码返回235
sprintf(m_sendbuff,"%s%s",base64(m_pass,ENCODE,strlen(m_pass)),"\r\n");
if(senddata()==0)
{
return0;
}
if(recvdata()==0)
{
return0;
}
if(strncmp(m_recvbuff,"235",3)!
=NULL)
{
m_error=PASS_ERROR;
return0;
}
return1;
}
intCMail:
:
connect()
{
WSADATAwsadata;
structsockaddr_insock_addr;
HOSTENT*phostent;
structin_addrinaddr;
if(WSAStartup(MAKEWORD(1,1),&wsadata)<0)
{
m_error=WSA_INIT_ERROR;
return0;
}
/*---------------根据域名获取网络地址,存放在structin_addr中----------------*/
phostent=gethostbyname(m_server);//
if(phostent!
=NULL)
{
//lpAddr=phostent->h_addr_list[0];
memmove(&inaddr,phostent->h_addr_list[0],4);//220.181.12.101
}
else
{
m_error=CONNECTSERVER_ERROR;
return0;
}
/*---------------根据域名获取网络地址,存放在structin_addr中----------------*/
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//SOCK_STREAMSOCK_DGRAM
sock_addr.sin_family=AF_INET;
sock_addr.sin_port=htons(m_port);
sock_addr.sin_addr=inaddr;//直接结构体赋值给结构体,或者写成:
sock_addr.sin_addr.s_addr=inaddr.S_un.S_addr;
if(:
:
connect(s,(structsockaddr*)&sock_addr,sizeof(sock_addr)))
{
m_error=CONNECT_ERROR;
return0;
}
return1;
}
intCMail:
:
senddata()
{
intidx=0;
intcsend;
intcount=strlen(m_sendbuff);
while(count>0)
{
if((csend=send(s,m_sendbuff+idx,count,0))==SOCKET_ERROR)
{
m_error=WSA_SEND_ERROR;
return0;
}
if(csend==0)
{
break;
}
count-=csend;
idx+=csend;
}
return1;
}
intCMail:
:
recvdata()
{
intcrecv;
crecv=recv(s,m_recvbuff,MAX_LENGTH,0);
if(crecv==SOCKET_ERROR)
{
m_error=WSA_RECV_ERROR;
return0;
}
elseif(crecv==0)
{
m_error=WSA_RECV_ERROR;
return0;
}
else
{
}
return1;
}
char*CMail:
:
base64(char*byte,enumcodeflagflag,intsize)/*base64编码函数*/
{//必须有参数size,因为编码的字节可能包括'\0',尤其是二进制文件
staticBYTEencodingTable[]={//index为64的'='是自己定义的
'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y',
'Z','a','b','c','d',
'e','f','g','h','i',
'j','k','l','m','n',
'o','p','q','r','s',
't','u','v','w','x',
'y','z','0','1','2',
'3','4','5','6','7',
'8','9','+','/'
};
inti;
chara,b,c,d;
char*coderesult;
char*decoderesult;
char*pa,*pb;
if(byte==NULL)
{
m_error=ARGUMENT_ERROR;
return0;
}
if(size==NULL)
{
m_error=ARGUMENT_ERROR;
return0;
}
pa=byte;
if(flag==ENCODE)//如果flag为ENCODE1,用base64编码
{
coderesult=(char*)LocalAlloc(LPTR,(size+2)/3*4+1);
intstatus=1;
for(pb=coderesult,i=0;i{
switch(status)
{
case1:
*pb=encodingTable[*pa>>2&63];
status=2;
break;
case2:
*pb=encodingTable[(*(pa-1)<<4&48)+(*pa>>4&15)];
status=3;
break;
case3:
*(pb++)=encodingTable[(*(pa-1)<<2&60)+(*pa>>6&3)];
*pb=encodingTable[*pa&63];
status=1;
break;
default:
break;
}
}
if(status==2)//size%3=1时
{
*(pb++)=encodingTable[*(pa-1)<<4&48];
*(pb++)='=';
*(pb++)='=';
}
elseif(status==3)//size%3=2时
{
*(pb++)=encodingTable[*(pa-1)<<2&60];
*(pb++)='=';
}
*pb='\0';//最后一个字节置0,字符串结束
returncoderesult;
}
else//flag为DECODE,解码
{
decoderesult=(char*)LocalAlloc(LPTR,size/4);
memset(decoderesult,0,size/4);
if(size%4!
=0)
{
m_error=ARGUMENT_ERROR;
return0;
}
for(pb=decoderesult;*pa;)
{
a=*(pa++);
b=*(pa++);
c=*(pa++);
d=*(pa++);
a=chartoindex(a);
b=chartoindex(b);
c=chartoindex(c);
d=chartoindex(d);
*(pb++)=(a<<2)+(b>>4&3);
*(pb++)=(b<<4)+(c>>2&15);//15为00001111
*(pb++)=(c<<6)+d;
}
*pb='\0';//最后一个字节置0,字符串结束
returndecoderesult;
}
}