此功能在嵌入式开发中常用来通知用户。 比如监控异常了,发邮件通知。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/fcntl.h> #include <sys/stat.h> #define SOCKET_ERROR -1 #define IPSTR "220.181.12.12" #define PRINT(x) printf("%s ",(x)) int sockfd; int ret; char *send_data; char *recv_data; char From[128]; char To[128]; char Date[128]; char Subject[512]; char Context[6000]={0}; char buffer[1024]; int connectHost(const char *smtpaddr) { struct sockaddr_in servaddr; sockfd = socket(AF_INET,SOCK_STREAM,0); if(sockfd < 0) { printf("Create socket error! "); return -1; } bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(25); if(inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ) { printf("inet_pton error! "); return -1; }; if (connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0) { printf("Connect failed... "); return -1; } printf("Connect to %s.... ", IPSTR); memset(buffer, 0, sizeof(buffer)); if(recv(sockfd, buffer, sizeof(buffer), 0) < 0) { printf("receive failed... "); return -1; } else { printf("%s ", buffer); } return sockfd; } int getResponse() { memset(buffer,0,sizeof(buffer)); ret = recv(sockfd,buffer,1024,0); if(ret == SOCKET_ERROR) { printf("receive nothing "); return -1; } buffer[ret]='