我的webf服务器是动态ip,因此需要检测服务器ip的变化然后更新域名的ip,打算检测到ip变化后自动发送邮件告知自己,于是上网搜了下编程发送邮件的方法,大部分是基于vc的,我找了个例子修改成了linux下的程序。这个程序是基于smtp协议的,smtp是基于tcp的,有兴趣的可以先自己看看smtp协议,这样看程序会更容易理解。
这个程序目前126的邮箱经测试成功发送,新浪的虽然显示发送成功但是总也收不到,有空再慢慢实验吧,其他邮箱还未测试。
程序如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <unistd.h> #include <netinet/in.h> #include <limits.h> #include <netdb.h> #include <arpa/inet.h> #include <ctype.h> // Function prototype void StreamClient(char *szServer, short nPort, char *szMessage); // Helper macro for displaying errors //////////////////////////////////////////////////////////// int main(int argc, char **argv) { char hostname[255]; int nRet; short nPort; nPort =25; StreamClient("smtp.126.com", nPort, "AUTH LOGIN\r\n"); return 0; } //////////////////////////////////////////////////////////// void StreamClient(char *szServer, short nPort, char *szMessage) { int sockfd; char buffer[1024]; struct sockaddr_in server_addr; struct hostent *host; if((host=gethostbyname(szServer))==NULL)/*取得主机IP地址*/ { fprintf(stderr,"Gethostname error, %s\n", strerror(errno)); exit(1); } if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)/*建立SOCKET连接*/ { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } /* 客户程序填充服务端的资料 */ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(nPort); server_addr.sin_addr=*((struct in_addr *)host->h_addr); /* 客户程序发起连接请求 */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*连接网站*/ { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } printf("\nStream Client connecting to server: %s on port: %d",szServer, nPort); char szBuf[1024]; memset(szBuf,0,1024); int nRet; // strcpy(szBuf, "From the Client"); char buf[350]="0"; char server[250]; gethostname(server,250); strcpy(buf, "HELO "); strcat(buf, szServer); strcat(buf, " \r\n"); printf("%s:::%d",buf,strlen(buf)); // // Wait for a reply // nRet = recv(sockfd,szBuf,sizeof(szBuf)+1,0); printf("\nData received OVER DATA: %s", szBuf); nRet = send(sockfd, buf, strlen(buf), 0);printf("\nsend %s",buf); nRet = recv(sockfd, szBuf, sizeof(szBuf), 0); printf("\nData received2: %s", szBuf); //发送准备登陆信息 nRet = send(sockfd, "AUTH LOGIN\r\n", strlen("AUTH LOGIN\r\n"), 0); nRet = recv(sockfd, szBuf, sizeof(szBuf), 0); printf("\nData received LOGIN: %s", szBuf); //发送用户名和密码,这里的用户名和密码必须用base64进行转码,发送转码以后的字符串,对于126邮箱来说用户名是@前面的字符串 nRet = send(sockfd,"转码后的用户名\r\n", strlen("转码后的用户名\r\n"),0);printf("\nData Send USERNAME"); nRet = recv(sockfd, szBuf, sizeof(szBuf),0);printf("\nData received USERNAME: %s", szBuf);printf("\nData Send PASS"); //发送用户密码 nRet = send(sockfd,"转码后的密码\r\n", strlen("转码后的密码\r\n"), 0); //printf("\nData Send PASS"); nRet = recv(sockfd, szBuf, sizeof(szBuf),0); printf("\nData received USERPASSWORD: %s", szBuf); //发送[发送邮件]的信箱(改成你的邮箱!),该邮箱要与用户名一致,否则发送不成功 send(sockfd,"MAIL FROM: <[email protected]>\r\n",strlen("MAIL FROM: <[email protected]>\r\n"),0);printf("\nsend MAIL FROM\n"); nRet = recv(sockfd, szBuf, sizeof(szBuf), 0);printf("\nData received MAILFROM: %s", szBuf); //发送[接收邮件]的邮箱 nRet= send(sockfd,"RCPT TO: <[email protected]>\r\n",strlen("RCPT TO: <[email protected]>\r\n"),0);printf("\nsend RCPT TO\r\n"); nRet = recv(sockfd, szBuf, sizeof(szBuf), 0); printf("\nData received TO MAIL: %s", szBuf); /* nRet= send(sockfd,"RCPT TO: <[email protected]>\r\n",strlen("RCPT TO: <[email protected]>\r\n"),0);printf("\nsend RCPT TO\r\n"); nRet = recv(sockfd, szBuf, sizeof(szBuf), 0); printf("\nData received TO MAIL: %s", szBuf); */ char MailData[] ="From: \"[email protected]\"<[email protected]>\r\nTo: [email protected]\r\nSubject: IP Address\r\n\r\n"; //主题可以改成别的 //各诉邮件服务器,准备发送邮件内容 send(sockfd,"DATA\r\n", strlen("DATA\r\n"),0);printf("\nsend DATA\n"); // nRet = recv(sockfd, szBuf, sizeof(szBuf)+1, 0); printf("\nData receivedSEND DATA: %s", szBuf); //发送邮件标题 send(sockfd,MailData, strlen(MailData),0); //发送邮件内容 send(sockfd,"test!!!!\r\n", strlen("abcdefg\r\n"),0);//我随便写了几个字符进行测试 //发送邮件结束 send(sockfd,"\r\n.\r\n", strlen("\r\n.\r\n"),0); //接收邮件服务器返回信息 nRet = recv(sockfd,szBuf,sizeof(szBuf),0); printf("\nData received OVER DATA: %s", szBuf); send(sockfd,"QUIT\r\n", strlen("QUIT\r\n"),0); nRet = recv(sockfd,szBuf,sizeof(szBuf),0); printf("\nData received QUIT: %s", szBuf); // // Display the received data // //printf("\nData received3: %s", szBuf); close(sockfd); return; } |
发表评论
要发表评论,您必须先登录。