#include <stdio.h> #include <string.h> #include <netdb.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<arpa/inet.h> //#include "send.h"
#define SOCKET_ERROR -1 #define PRINT(x) printf("%s\n",(x)) int sockfd; int ret; char *send_data; char *recv_data; char From[128]; char To[128]; char Cc[128]; char Date[128]; char Subject[512]; char Context[6000]={0}; char buffer[1024]; int connectHost(const char *smtpaddr) { struct sockaddr_in servaddr; struct hostent *pHostent; struct in_addr ** pptr; sockfd=socket(AF_INET,SOCK_STREAM,0); if (sockfd<0) { fprintf(stderr,"Create socket error!\n"); return -1; } bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(25); printf("%s\n",smtpaddr); pHostent = gethostbyname(smtpaddr); //Get the Mail Server
if(pHostent == NULL) { fprintf(stderr,"The Host Name is Invalid... \n"); return -1; } pptr =(struct in_addr **)pHostent->h_addr_list; memcpy(&servaddr.sin_addr,*pptr,sizeof(struct in_addr)); //connect the Mail server
if (connect(sockfd,(struct sockaddr *)& servaddr,sizeof(servaddr)) < 0) { fprintf(stderr,"Connect failed... \n"); return -1; }
printf("Connect to %s.... \n", inet_ntoa(servaddr.sin_addr)); /*memset(buffer, 0,sizeof(buffer)); if(recv(sockfd, buffer, sizeof(buffer), 0) < 0) { fprintf(stderr,"Failed to connect. \n"); return -1; }else{ printf("%s\n123", buffer); }*/ if(getResponse() < 0) return -1; return sockfd; }
int getResponse() { memset(buffer,0,sizeof(buffer)); ret = recv(sockfd,buffer,1024,0); if(ret == SOCKET_ERROR) { printf("receive nothing\n"); return -1; } buffer[ret]='\0'; if(*buffer == '5') { printf("the order is not support smtp host\n "); return -1; } printf("%s\n",buffer); return 0; }
int login(char *username,char *password) { printf("sockfd in login() %d\n",sockfd); char ch[100]; if(username == "" || password == "") return -1; send_data = "HELO server\r\n"; printf("%s\n",send_data); ret = send(sockfd,send_data,strlen(send_data),0); if(ret == SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; send_data = "AUTH LOGIN\r\n"; printf("%s\n",send_data); ret = send(sockfd,send_data,strlen(send_data),0); if(ret == SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; sprintf(ch,"%s\r\n",username); printf("%s\n",ch); ret = send(sockfd,(char *)ch,strlen(ch),0); if(ret == SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; sprintf(ch,"%s\r\n",password); printf("%s\n",ch); ret = send(sockfd,(char *)ch,strlen(ch),0); if(ret == SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; return 0;
}
int sendmail(char* from,char * to,char * cc,char * subject,char * context) { if(from =="" ||to == ""||cc == ""||subject == ""||context == "") { fprintf(stderr,"arguments error!\n"); return -1; } /*MAIL FROM*/ sprintf(From,"MAIL FROM: <%s>\r\n",from); printf("%s\n",From); if((ret= send(sockfd,From,strlen(From),0))== SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; /*RCPT TO*/ sprintf(To,"RCPT TO: <%s>\r\n",to); PRINT(to); PRINT(To); if((ret= send(sockfd,To,strlen(To),0))== SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; /*DATA*/ send_data = "DATA\r\n"; if((ret= send(sockfd,send_data,strlen(send_data),0))== SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1; /*From :*/ memset(From,0,sizeof(From)); sprintf(From,"From: %s\r\n",from); strcat(Context,From);
/*To :*/ memset(To,0,sizeof(To)); sprintf(To,"To: %s\r\n",to); strcat(Context,To); /*Cc :*/ memset(Cc,0,sizeof(Cc)); sprintf(Cc,"Cc: %s\r\n",cc);
strcat(Context,Cc); /*Subject :*/ memset(Subject,0,sizeof(Subject)); sprintf(Subject,"Subject: %s\r\n",subject); strcat(Context,Subject); /*"\r\n\r\n"*/ strcat(Context,"\r\n\r\n"); strcat(Context,context); /* . */ strcat(Context,"\r\n.\r\n"); if((ret= send(sockfd,Context,strlen(Context),0))== SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1;
/*QUIT*/ printf("QUIT!!!!!!!!!!!!!!!!\n"); if((ret= send(sockfd,"QUIT\r\n",strlen("QUIT\r\n"),0))== SOCKET_ERROR) { close(sockfd); return -1; } if(getResponse() < 0) return -1;
printf("Send Mail Successful!\n"); return 0; }
int main() { int sockfd; struct sockaddr_in servaddr; struct hostent *pHostent; struct in_addr ** pptr; char *buffer[1024]={0}; char from[128]="xiaoping4220@163.com"; char to[128]="50284191@qq.com"; char cc[128]="xiaoping4220@gmail.com"; //char date[128]={0};
char subject[512]="50284191@qq.com"; char context[6000]="Just a test !!!!!!!!!!!\n"; char server[56]="smtp.163.com"; char *date =ctime(gmtime()); char *name="eGl45656w";/*Base64encoding*/ char *passwd= "O4456456w";/*Base64encoding*/
if (connectHost(server)<0) { fprintf(stderr,"Can Not LOGIN !\n"); return -1; }
printf("%d\n",sockfd); if(login(name,passwd) < 0) { fprintf(stderr,"Can Not LOGIN !\n"); return -1; } sendmail(from,to,cc,subject,context); return 0; }
另外对前2天的短信网关发送程序做一点总结,
难度2个方面,第一短信PDU码编码上,要注意字节转换 Stringtobytes 。
第二在于LINUX下的终端配置,这个让我头大惨了。。 经修改gnokii源码,然后用stty -a
经修改后还是要出问题。崩溃。。。
项目上决定直接采用gnokii
波特率115200,8N1,
只需装一个gnokii 10行代码就可搞定我200行代码的问题。
方便,我何必自找苦吃呢!
|