H
hxy2002
Unregistered / Unconfirmed
GUEST, unregistred user!
那位知道SMTP协议命令原始码及工作原理?请多多指教。
//SMTP_VC.CPP
#include "stdafx.h"
#include <winsock2.h>
#include <string.h>
#include <stdio.h>
char szhost[17]="211.150.96.25";
//char szhost[17]="127.0.0.1";
char szMessage[1024];
int checkreturn(char *_msg);
int postcmd(SOCKET smtp,char * cmd,char * ret,bool rec);
int base64enc(char *,char *);
int main(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// printf("%s",lpCmdLine);
//MessageBox(0,lpCmdLine,"jk",MB_OK);
WSADATA wsd;
SOCKET smtp;
int ret;
sockaddr_in server;
hostent *host=NULL;
char cmd[100];
if (WSAStartup(MAKEWORD(2,2),&wsd)!=0) //初始化
{
MessageBox(0,"错误1","错误",MB_OK);
return 1;
}
smtp=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (smtp==INVALID_SOCKET)
{
MessageBox(0,"错误2","错误",MB_OK);
return 2;
}
server.sin_family=AF_INET;
server.sin_port=htons(25);
server.sin_addr.s_addr=inet_addr(szhost);
if(connect(smtp,(struct sockaddr*)&server,sizeof(server))==SOCKET_ERROR)
{
//MessageBox(0,"连接错误","错误",MB_OK);
printf("连接错误/n");
return 3;
}
else
{
//MessageBox(0,"连接成功","成功",MB_OK);
printf("连接成功/n");
}
ret=recv(smtp,szMessage,1024,0);
/* sprintf(cmd,"HELO");
if (postcmd(smtp,cmd,szMessage,true)!=250)
{
printf("HELO ERROR/n");
return 1;
}*/
sprintf(cmd,"AUTH LOGIN");
if (postcmd(smtp,cmd,szMessage,true)!=334)
{
printf("auth login error/n");
return 1;
}
base64enc(cmd,"cuilun"); //用户名base64编码
//printf("%s",szMessage);
postcmd(smtp,cmd,szMessage,true);
base64enc(cmd,"password"); //密码
if(postcmd(smtp,cmd,szMessage,true)!=235)
{
printf("认证失败!/n");
return 1;
}
sprintf(cmd,"MAIL FROM:from@..."); //来自
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"RCPT TO: "); //收信
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"data");
postcmd(smtp,cmd,szMessage,true);
sprintf(cmd,"from://");////////
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"to:。。。");///////////////////
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"subject:"); ///////////////
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"This is Only a TEST!");
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"test");
postcmd(smtp,cmd,szMessage,false);
sprintf(cmd,"%c%c.",13,10);
if(postcmd(smtp,cmd,szMessage,true)==250)
{
printf("发送成功!/n");
}
else
{
printf("发送失败!/n");
}
//printf("/n%c/n",(('u'<<6)>>2));
//sprintf(cmd,"%c%c",13,10);
closesocket(smtp);
WSACleanup();
return 0;
}
int checkreturn(char *_msg)
{
int i;
int result=0;
int t=1;
for (i=2;i>=0;i--)
{
result+=(_msg[i]-'0')*t;
t=t*10;
}
return result;
}
int base64enc(char *_dest,char *_source) //编码,烦死了。
{
int len=strlen((char*)_source);
int i,j,k;
int count=len/3;
unsigned char *dest=(unsigned char*)_dest;
unsigned char *source=(unsigned char*)_source;
unsigned char temp[3]={0,0,0};
j=0;
for(i=0;i<count;i++)
{
dest[j]=source[i*3]/4;
dest[j+1]=source[i*3]%4*16+source[i*3+1]/16;
dest[j+2]=source[i*3+1]%16*4+source[i*3+2]/64;
dest[j+3]=source[i*3+2]%64;
j=j+4;
}
if(source[i*3]!=0)
{
for (k=0;k<=2;k++)
{
temp[k]=0;
}
for(k=0;k<=2;k++)
{
if(source[i*3+k]!=0) temp[k]=source[i*3+k];
}
dest[j]=temp[0]/4;
dest[j+1]=temp[0]%4*16+temp[1]/16;
if (temp[1]!=0)
dest[j+2]=temp[1]%16*4+temp[2]/64;
else
dest[j+2]=127;
if (temp[2]!=0)
dest[j+3]=temp[2]%64;
else
dest[j+3]=127;
j=j+4;
}
for(i=0;i<j;i++)
{
if (dest[i]>=0&&dest[i]<=25)
{
dest[i]+='A';
continue;
}
if (dest[i]>=26&&dest[i]<=51)
{
dest[i]=dest[i]-26+'a';
continue;
}
if (dest[i]>=52&&dest[i]<=61)
{
dest[i]=dest[i]-52+'0';
continue;
}
if (dest[i]==62)
{
dest[i]='+';
continue;
}
if (dest[i]==63)
{
dest[i]='/';
continue;
}
if(dest[i]==127)
{
dest[i]='=';
continue;
}
printf("/nencode error/n");
}
dest[j]=0;
return j;
}
int postcmd(SOCKET smtp,char * cmd,char *ret,bool rec)
{
printf("发送:%s/n",cmd);
int len=strlen(cmd);
cmd[len]=13;cmd[len+1]=10;cmd[len+2]=0;
send(smtp,cmd,strlen(cmd),0);
if (rec){
recv(smtp,ret,1024,0);
printf("接收:%s/n",ret);
return checkreturn(ret);
}
return 0;
}