- 论坛徽章:
- 0
|
- #include <iostream>;
- #include <string>;
- #include <sys/socket.h>;
- #include <netdb.h>;
- #include <errno.h>;
- #include <unistd.h>;
- using namespace std;
- const int PORT = 25;
- int main(void)
- {
-
- int sockfd;
- struct sockaddr_in mail_serv_addr;
- struct sockaddr_in mail_clit_addr;
- struct hostent *ser_name;
- char cmd_str[512];
- char server_msg[512];
- memset(&cmd_str,'\0',512);
- memset(&server_msg,'\0',512);
- ser_name = gethostbyname("localhost");
-
- /*建立socket连接*/
- if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
- {
- fprintf(stderr,"socket error: %s",strerror(errno));
- exit(1);
- }
- /*客户端填充内容*/
- memset(&mail_serv_addr,'\0',sizeof(struct sockaddr_in));
- mail_serv_addr.sin_family = AF_INET;
- mail_serv_addr.sin_port = htons(PORT);
- mail_serv_addr.sin_addr = *((struct in_addr *)ser_name->;h_addr);
-
- /*连接邮件服务器*/
- if (connect(sockfd,(struct sockaddr *)&mail_serv_addr,sizeof(struct sockaddr)) < 0)
- {
- fprintf(stderr,"connect error: %s\n",strerror(errno));
- exit(1);
- }
- /*连接成功*/
- memset(server_msg,0,512);
- memset(cmd_str,0,512);
- if (read(sockfd,server_msg,512) < 0)
- {
- fprintf(stderr,"read error!\n");
- exit(1);
- }
- if (strstr(server_msg,"220") == NULL)
- {
- fprintf(stderr,"error: %s",server_msg);
- exit(1);
- }
- else
- {
- memset(server_msg,0,512);
- memset(cmd_str,0,512);
- sprintf(cmd_str,"HELO localhost\r\n");
- if (write(sockfd,cmd_str,512) < 0)
- {
- fprintf(stderr,"write error!\n");
- exit(1);
- }
- if (read(sockfd,server_msg,512) <0)
- {
- fprintf(stderr,"read error!\n");
- exit(1);
- }
- if (strstr(server_msg,"250") == NULL)
- {
- fprintf(stderr,"error: %s\n",server_msg);
- exit(1);
- }
- else
- {
- memset(server_msg,0,512);
- memset(cmd_str,0,512);
- sprintf(cmd_str,"mail from: kofwang@sina.com\r\n");
- if (write(sockfd,cmd_str,strlen(cmd_str)) < 0)
- {
- fprintf(stderr,"write error: %s\n",strerror(errno));
- exit(1);
- }
- if (read(sockfd,server_msg,512) < 0)
- {
- fprintf(stderr,"read error: %s\n",strerror(errno));
- exit(1);
- }
- if (strstr(server_msg,"250") == NULL)
- {
- fprintf(stderr,"error: %s\n",server_msg);
- exit(1);
- }
- else
- {
- memset(server_msg,0,512);
- memset(cmd_str,0,512);
- sprintf(cmd_str,"rcpt to: kofwang@hotmail.com\r\n");
- if (write(sockfd,cmd_str,512) < 0)
- {
- fprintf(stderr,"write error: %s\n",strerror(errno));
- exit(1);
- }
- if (read(sockfd,server_msg,512) < 0)
- {
- fprintf(stderr,"read error: %s\n",strerror(errno));
- exit(1);
- }
- if (strstr(server_msg,"250") == NULL)
- {
- fprintf(stderr,"error: %s\n",server_msg);
- exit(1);
- }
- else
- {
- memset(server_msg,'\0',512);
- memset(cmd_str,'\0',512);
- sprintf(cmd_str,"data\r\n");
- if (write(sockfd,cmd_str,512) < 0)
- {
- fprintf(stderr,"write error: %s\n",strerror(errno));
- exit(1);
- }
- if (read(sockfd,server_msg,512) < 0)
- {
- fprintf(stderr,"read error: %s\n",strerror(errno));
- exit(1);
- }
- if (strstr(server_msg,"354") == NULL)
- {
- fprintf(stderr,"error: %s\n",server_msg);
- exit(1);
- }
- else
- {
- cout<<"success!"<<endl;
- return 1;
- /*书写邮件*/
- }
- }
- }
- }
- }
- }
-
复制代码
将“mail from: kofwang@sina.com"发送到服务端后,服务端返回错误,大致意思是无法识别的命令:"",我分析是之前发送了一个回车给服务端,可是我找不到回车出自哪里啊?大家帮我分析分析!谢谢! |
|