C语言利用SMTP协议发送邮件
1 #ifdef WIN32 2 #include <windows.h> 3 #include <stdio.h> 4 #else 5 6 #include <stdio.h> 7 #include <sys/types.h> 8 #include <sys/socket.h> 9 #include <netinet/in.h> 10 #include <errno.h> 11 #include <unistd.h> 12 #include <sys/time.h> 13 #endif 14 struct data6 15 { 16 unsigned int d4:6; 17 unsigned int d3:6; 18 unsigned int d2:6; 19 unsigned int d1:6; 20 }; 21 // 协议中加密部分使用的是base64方法 22 char con628(char c6); 23 void base64(char *dbuf,char *buf128,int len); 24 void sendemail(char *email,char *body); 25 int open_socket(struct sockaddr *addr); 26 27 int main() 28 { 29 char email[] = "987654321@qq.com"; 30 char body[] = "From: \"lucy\"<123456789@qq.com>\r\n" 31 "To: \"dasiy\"<987654321@qq.com>\r\n" 32 "Subject: Hello\r\n\r\n" 33 "Hello World, Hello Email!"; 34 sendemail(email, body); 35 return 0; 36 } 37 38 char con628(char c6) 39 { 40 char rtn = '\0'; 41 if (c6 < 26) rtn = c6 + 65; 42 else if (c6 < 52) rtn = c6 + 71; 43 else if (c6 < 62) rtn = c6 - 4; 44 else if (c6 == 62) rtn = 43; 45 else rtn = 47; 46 return rtn; 47 } 48 49 // base64的实现 50 void base64(char *dbuf, char *buf128, int len) 51 { 52 struct data6 *ddd = NULL; 53 int i = 0; 54 char buf[256] = {0}; 55 char *tmp = NULL; 56 char cc = '\0'; 57 memset(buf, 0, 256); 58 strcpy(buf, buf128); 59 for(i = 1; i <= len/3; i++) 60 { 61 tmp = buf+(i-1)*3; 62 cc = tmp[2]; 63 tmp[2] = tmp[0]; 64 tmp[0] = cc; 65 ddd = (struct data6 *)tmp; 66 dbuf[(i-1)*4+0] = con628((unsigned int)ddd->d1); 67 dbuf[(i-1)*4+1] = con628((unsigned int)ddd->d2); 68 dbuf[(i-1)*4+2] = con628((unsigned int)ddd->d3); 69 dbuf[(i-1)*4+3] = con628((unsigned int)ddd->d4); 70 } 71 if(len%3 == 1) 72 { 73 tmp = buf+(i-1)*3; 74 cc = tmp[2]; 75 tmp[2] = tmp[0]; 76 tmp[0] = cc; 77 ddd = (struct data6 *)tmp; 78 dbuf[(i-1)*4+0] = con628((unsigned int)ddd->d1); 79 dbuf[(i-1)*4+1] = con628((unsigned int)ddd->d2); 80 dbuf[(i-1)*4+2] = '='; 81 dbuf[(i-1)*4+3] = '='; 82 } 83 if(len%3 == 2) 84 { 85 tmp = buf+(i-1)*3; 86 cc = tmp[2]; 87 tmp[2] = tmp[0]; 88 tmp[0] = cc; 89 ddd = (struct data6 *)tmp; 90 dbuf[(i-1)*4+0] = con628((unsigned int)ddd->d1); 91 dbuf[(i-1)*4+1] = con628((unsigned int)ddd->d2); 92 dbuf[(i-1)*4+2] = con628((unsigned int)ddd->d3); 93 dbuf[(i-1)*4+3] = '='; 94 } 95 return; 96 } 97 // 发送邮件 98 void sendemail(char *email, char *body) 99 { 100 int sockfd = 0; 101 struct sockaddr_in their_addr = {0}; 102 char buf[1500] = {0}; 103 char rbuf[1500] = {0}; 104 char login[128] = {0}; 105 char pass[128] = {0}; 106 #ifdef WIN32 107 WSADATA WSAData; 108 WSAStartup(MAKEWORD(2, 2), &WSAData); 109 #endif 110 memset(&their_addr, 0, sizeof(their_addr)); 111 their_addr.sin_family = AF_INET; 112 their_addr.sin_port = htons(25); 113 their_addr.sin_addr.s_addr = inet_addr("112.90.141.71");//qq smtp 服务器 114 // 连接邮件服务器,如果连接后没有响应,则2 秒后重新连接 115 sockfd = open_socket((struct sockaddr *)&their_addr); 116 memset(rbuf,0,1500); 117 while(recv(sockfd, rbuf, 1500, 0) == 0) 118 { 119 printf("reconnect...\n"); 120 Sleep(2); 121 //close(sockfd); 122 sockfd = open_socket((struct sockaddr *)&their_addr); 123 124 memset(rbuf,0,1500); 125 } 126 127 printf("%s\n", rbuf); 128 129 // EHLO 130 memset(buf, 0, 1500); 131 sprintf(buf, "EHLO abcdefg-PC\r\n"); 132 send(sockfd, buf, strlen(buf), 0); 133 memset(rbuf, 0, 1500); 134 recv(sockfd, rbuf, 1500, 0); 135 printf("%s\n", rbuf); 136 137 // AUTH LOGIN 138 memset(buf, 0, 1500); 139 sprintf(buf, "AUTH LOGIN\r\n"); 140 send(sockfd, buf, strlen(buf), 0); 141 printf("%s\n", buf); 142 memset(rbuf, 0, 1500); 143 recv(sockfd, rbuf, 1500, 0); 144 printf("%s\n", rbuf); 145 146 // USER 147 memset(buf, 0, 1500); 148 sprintf(buf,"12345678");//你的qq号 149 memset(login, 0, 128); 150 base64(login, buf, strlen(buf)); 151 sprintf(buf, "%s\r\n", login); 152 send(sockfd, buf, strlen(buf), 0); 153 printf("%s\n", buf); 154 memset(rbuf, 0, 1500); 155 recv(sockfd, rbuf, 1500, 0); 156 printf("%s\n", rbuf); 157 158 // PASSWORD 159 sprintf(buf, "qqpassword");//你的qq密码 160 memset(pass, 0, 128); 161 base64(pass, buf, strlen(buf)); 162 sprintf(buf, "%s\r\n", pass); 163 send(sockfd, buf, strlen(buf), 0); 164 printf("%s\n", buf); 165 166 memset(rbuf, 0, 1500); 167 recv(sockfd, rbuf, 1500, 0); 168 printf("%s\n", rbuf); 169 170 // MAIL FROM 171 memset(buf, 0, 1500); 172 sprintf(buf, "MAIL FROM: <123456789@qq.com>\r\n"); 173 send(sockfd, buf, strlen(buf), 0); 174 memset(rbuf, 0, 1500); 175 recv(sockfd, rbuf, 1500, 0); 176 printf("%s\n", rbuf); 177 178 // RCPT TO 第一个收件人 179 sprintf(buf, "RCPT TO:<%s>\r\n", email); 180 send(sockfd, buf, strlen(buf), 0); 181 memset(rbuf, 0, 1500); 182 recv(sockfd, rbuf, 1500, 0); 183 printf("%s\n", rbuf); 184 185 // DATA 准备开始发送邮件内容 186 sprintf(buf, "DATA\r\n"); 187 send(sockfd, buf, strlen(buf), 0); 188 memset(rbuf, 0, 1500); 189 recv(sockfd, rbuf, 1500, 0); 190 printf("%s\n", rbuf); 191 192 // 发送邮件内容,\r\n.\r\n内容结束标记 193 sprintf(buf, "%s\r\n.\r\n", body); 194 send(sockfd, buf, strlen(buf), 0); 195 memset(rbuf, 0, 1500); 196 recv(sockfd, rbuf, 1500, 0); 197 printf("%s\n", rbuf); 198 199 // QUIT 200 sprintf(buf, "QUIT\r\n"); 201 send(sockfd, buf, strlen(buf), 0); 202 memset(rbuf, 0, 1500); 203 recv(sockfd, rbuf, 1500, 0); 204 printf("%s\n", rbuf); 205 206 // VC2005 需要使用 207 closesocket(sockfd); 208 // close(sockfd); 209 #ifdef WIN32 210 WSACleanup(); 211 #endif 212 return; 213 214 } 215 // 打开TCP Socket连接 216 int open_socket(struct sockaddr *addr) 217 { 218 int sockfd = 0; 219 sockfd=socket(PF_INET, SOCK_STREAM, 0); 220 if(sockfd < 0) 221 { 222 fprintf(stderr, "Open sockfd(TCP) error!\n"); 223 exit(-1); 224 } 225 if(connect(sockfd, addr, sizeof(struct sockaddr)) < 0) 226 { 227 fprintf(stderr, "Connect sockfd(TCP) error!\n"); 228 exit(-1); 229 } 230 return sockfd; 231 }
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/3414518.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2013-11-08 16:25 jack_Meng 阅读(9491) 评论(0) 编辑 收藏 举报