typedef struct mail_tag{
char mail_addr[255];
char mail_from[255];
char mail_to[255];
char username[20];
char password[20];
char subject[255];
char content[1024];
} MAIL;
int sendmail (MAIL *pmail)
{
char *pmail_addr=pmail->mail_addr;
char *pmail_from=pmail->mail_from;
char *pmail_to=pmail->mail_to;
char *pusername=pmail->username;
char *ppassword=pmail->password;
char pdata[1048]={0};
strcpy(pdata,"from:");
strcat(pdata,pmail->mail_from);
strcat(pdata,"/r/n");
strcat(pdata,"to:");
strcat(pdata,pmail->mail_to);
strcat(pdata,"/r/n");
strcat(pdata,"subject:");
strcat(pdata,pmail->subject);
strcat(pdata,"/r/n/r/n");
strcat(pdata,pmail->content);
strcat(pdata,"/r/n");
//构造邮件数据
Base64 bs;
WSADATA data;
unsigned short version;
version=0x0002; //version 2.0版本
::WSAStartup(version,&data);
sockaddr_in mail_addr={0};
hostent *phost=::gethostbyname(pmail_addr);
mail_addr.sin_addr.S_un.S_addr=*(unsigned long *)phost->h_addr_list[0];
mail_addr.sin_family=AF_INET;
mail_addr.sin_port=htons(25);
SOCKET s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
int ret=connect(s,(sockaddr*)&mail_addr,sizeof(mail_addr));
if(ret<0)
{
quit(s);
return 1;
}
char buffer[255]={0};
recv(s,buffer,255,0);
memset(buffer,0,255);
//开始登陆smtp服务器
ret=send(s,"HELO LLIKZ/r/n",12,0);
//发送HELO
if(ret<=0)
{
quit(s);
return 1;
}
ret=recv(s,buffer,255,0);
if(ret<=0)
{
quit(s);
return 1;
}
memset(buffer,0,255);
//发送身份认证请求
send( s,"AUTH LOGIN/r/n",12,0);
recv(s,buffer,255,0);
if(buffer[0]!='3'&&buffer[1]!='3')
{
quit(s);
return 1;
}
memset(buffer,0,255);
strcpy(buffer,bs.base64_encode( pusername).c_str());
strcat( buffer,"/r/n");
//发送BASE64编码的用户名
send(s,buffer,strlen(buffer),0);
memset(buffer,0,255);
recv(s,buffer,255,0);
if(buffer[0]!='3')
{
quit(s);
return 1;
}
memset(buffer,0,255);
//发送BASE64编码的密码
strcpy(buffer,bs.base64_encode(ppassword).c_str());
strcat(buffer,"/r/n");
send(s,buffer,strlen(buffer),0);
memset(buffer,0,255);
recv(s,buffer,255,0);
if(buffer[0]!='2'&&buffer[1]!='3')
{
quit(s);
return 1;
}
//认证完毕,开始发送邮件数据
memset(buffer,0,255);
strcpy(buffer,"MAIL FROM:<");
strcat(buffer,pmail_from);
strcat(buffer,">");
strcat(buffer,"/r/n");
//发送mail from命令
send(s,buffer,strlen(buffer),0);
memset(buffer,0,255);
recv(s,buffer,255,0);
if(buffer[0]!='2'&&buffer[1]!='5')
{
quit(s);
return 1;
}
memset(buffer,0,255);
strcpy(buffer,"RCPT TO:<");
strcat(buffer,pmail_to);
strcat(buffer,">");
strcat(buffer,"/r/n");
//发送rcpt to指令
send(s,buffer,strlen(buffer),0);
memset(buffer,0,255);
recv(s,buffer,255,0);
if(buffer[0]!='2'&&buffer[1]!='5')
{
quit(s);
return 1;
}
memset(buffer,0,255);
send(s,"DATA/r/n",6,0);
recv(s,buffer,255,0);
if(buffer[0]!='3'&&buffer[1]!='5')
{
quit(s);
return 1;
}
strcpy(buffer,pdata);
strcat(buffer,"/r/n./r/n");
//开始发送邮件数据
send(s,buffer,strlen(buffer),0);
memset(buffer,0,255);
recv(s,buffer,255,0);
if(buffer[0]!='2')
{
quit(s);
return 1;
}
memset(buffer,0,255);
strcpy(buffer,"QUIT/r/n");
//退出登陆
send(s,buffer,strlen(buffer),0);
closesocket(s);
::WSACleanup();
return 0;
}
VC实现邮件发送
最新推荐文章于 2021-11-14 18:26:39 发布