问题解决了。
4 E! K) Y/ a; n! L! P. h) I5 |以下是代码,写得很烂,本人是初学者大家就讲究着看吧,有问题多多指教下!
复制内容到剪贴板
代码:
#include <netinet/in.h>//include socket
#include <sys/socket.h>
#include <stdio.h>//include fopen(),fread().fwrite(),fclose()
#include <stdlib.h>//include system()
#include <netdb.h>//include gethostbyname()
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#define PORT 25 //smtp port
#define SIZE 1024
//define mail commands
#define EHLO 0
#define AUTH 1
#define USER 2
#define PASS 3
#define MAIL 4
#define RCPT 5
#define DATA 6
#define CONT 7
#define QUIT 8
void base64enc( char *buf,const char *text,int size);
int main(int argc,char *argv[])
{
int c;
int sockfd;
struct sockaddr_in server_addr;
struct hostent *server_ip;
int numbytes=0,i=0;
char username[30]="";//mail username
char passwd[30]="";//mail passwd
char str1[512]="";
char str2[512]="";
//buff store data by recv(),
//ip[SIZE] store data by fread() from ip_files(use "ifconfig >tmp.ip",het ip_files)
char buff[512]="",tmp[4]="",ip[SIZE]="";
int ret=0;//function return
FILE *f_open,*f_write;
char *msg[9]={""};
char *n_return[9]={""}; //return number
char *p;
msg[EHLO]="ehlo smtp.21cn.com\n";
msg[AUTH]="auth login\n";
base64enc(username,str1,strlen(str1));
strcat(username,"\n");
msg[USER]=username;
base64enc(passwd,str1,strlen(str1));
strcat(passwd,"\n");
msg[PASS]=passwd;
msg[MAIL]="mail from:xxxxx@163.com\n";
msg[RCPT]="rcpt to:xxxxx@sohu.com\n";
msg[DATA]="data\n";
msg[QUIT]="quit\n";
n_return[EHLO]="250";
n_return[AUTH]="334";
n_return[USER]="334";
n_return[PASS]="235";
n_return[MAIL]="250";
n_return[RCPT]="250";
/*---------------------------
copy self to /bin/getip
---------------------------*/
while((server_ip=gethostbyname(argv[1]))==NULL)
{
herror("gethostbyname error");
}
//create a socket
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return(-1);
}
//address information
server_addr.sin_family=AF_INET;//host byte order
server_addr.sin_port=htons(PORT);//short,network byte order
server_addr.sin_addr=*((struct in_addr *)server_ip->h_addr);//server ip
bzero(&(server_addr.sin_zero),8);//zero the rest of struct
//connect server
if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("connect error");
return(-1);
}
//if connect success,server return "220"
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("recv error");
return(-1);
}
printf("%s",buff);
//send msgs. if any step has a mistake,the "while" will be breaked,then send "quit" to end connection
while(i!=QUIT)
{
scanf("%d",&i);
getchar();
while(i<0||i>6)
{
scanf("%d",&i);
}
switch(i)
{
case 0: i=EHLO;break;
case 1: i=AUTH;break;
case 2: i=USER;break;
case 3: i=PASS;break;
case 4: i=MAIL;break;
case 5: i=RCPT;break;
case 6: i=DATA;break;
case 7: i=QUIT;break;
}
if(i==EHLO)
{
if((numbytes=send(sockfd,"ehlo smtp.21cn.com\n",19,0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
}
else if(i==AUTH)
{
if((numbytes=send(sockfd,"auth login\n",11,0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
}
else if(i==USER)
{
scanf("%s",str1);
base64enc(username,str1,strlen(str1));
strcat(username,"\n");
if((numbytes=send(sockfd,username,strlen(username),0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
}
else if(i==PASS)
{
scanf("%s",str1);
base64enc(passwd,str1,strlen(str1));
strcat(passwd,"\n");
if((numbytes=send(sockfd,passwd,strlen(passwd),0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
}
else if(i==MAIL)
{
scanf("%s",str2);
sprintf(str1,"mail from:<%s>\n",str2);
// strcat(str2," from:<longwei46@21cn.com>\n");
if((numbytes=send(sockfd,str1,strlen(str1),0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
}
else if(i==RCPT)
{
scanf("%s",str2);
sprintf(str1,"rcpt to:<%s>\n",str2);
if((numbytes=send(sockfd,str1,strlen(str1),0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
}
else if(i==DATA)
{
if((numbytes=send(sockfd,"data\n",5,0))==-1)
{
perror("send error");
break;
}
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
printf("command:%s\n",msg[i]);
// printf("command:%s\n",msg[i]);
printf("return buff:%s\n",buff);
scanf("%s",str2);
strcat(str2,"\n.\n");
if((numbytes=send(sockfd,str2,strlen(str2),0))==-1)
{
perror("send error");
break;
}
/* if((numbytes=send(sockfd,"mail from:longwei46@21cn.com\n",29,0))==-1)
{
perror("send error");
break;
}
* if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
printf("command:%s\n",msg[i]);
printf("return buff:%s\n",buff);*/
if((numbytes=recv(sockfd,buff,SIZE,0))==-1)
{
perror("send error");
break;
}
printf("command:%s\n",msg[i]);
printf("return buff:%s\n",buff);
//eleep(1);we dont have to use it,because recv() can choke itself until it received data
}
printf("command:%s\n",msg[i]);
// printf("command:%s\n",msg[i]);
printf("return buff:%s\n",buff);
//printf("should return:%s\n",n_return);
}
//send quit to end mail connection
if((numbytes=send(sockfd,msg[QUIT],strlen(msg[QUIT]),0))==-1)
{
perror("send error");
return(-1);
}
close(sockfd);
return (0);
}
void base64enc( char *out,const char *in,int inlen)
{
static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for(;inlen >= 3;inlen -= 3)
{
*out++ = base64_encoding[in[0] >> 2];
*out++ = base64_encoding[((in[0] << 4) & 0x30) | (in[1] >> 4)];
*out++ = base64_encoding[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
*out++ = base64_encoding[in[2] & 0x3f];
in +=3;
}
if(inlen > 0)
{
unsigned char fragment;
*out++ = base64_encoding[in[0] >> 2];
fragment = (in[0] << 4) & 0x30;
if(inlen > 1)
fragment |= in[1] >> 4;
*out++ = base64_encoding[fragment];
*out++ = (inlen < 2) ? '=' : base64_encoding[(in[1] << 2) & 0x3c];
*out++ = '=';
}
*out = '\0';
}