发送邮件

ぃ、小莉子 提交于 2020-02-12 12:03:29

效果图

 

 

 

 

main.cpp

 1 #include <iostream>
 2 #include "SendMail.h"
 3 #include <string>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     char EmailContents[255]="From: \"名字\"<xxxxxxx@163.com>\r\nTo:";
 9     char EmailTo[50];
10     char subject[50];
11     char content[50];
12     printf("要发送的邮箱账号:");
13     scanf("%s",EmailTo);
14     printf("你的标题:");
15     scanf("%s",subject);
16     printf("你的内容:");
17     scanf("%s",content);
18 
19     strcat(EmailContents,EmailTo);
20     strcat(EmailContents,"\n");
21     strcat(EmailContents, "subject:");
22     strcat(EmailContents, subject);
23     strcat(EmailContents, "\r\n\r\n");
24     strcat(EmailContents, content);
25     strcat(EmailContents, "\r\n.\r\n\0");
26     SendMail(EmailTo, EmailContents);
27     return 0;
28 }

SendMail.cpp

  1 #include "SendMail.h"
  2 
  3 #pragma  comment(lib, "ws2_32.lib")
  4 #include <string.h>
  5 struct Base64Date6
  6 {
  7     unsigned int d4 : 6;
  8     unsigned int d3 : 6;
  9     unsigned int d2 : 6;
 10     unsigned int d1 : 6;
 11 };
 12 
 13 char ConvertToBase64(char uc)
 14 {
 15     if (uc < 26)
 16     {
 17         return 'A' + uc;
 18     }
 19     if (uc < 52)
 20     {
 21         return 'a' + (uc - 26);
 22     }
 23     if (uc < 62)
 24     {
 25         return '0' + (uc - 52);
 26     }
 27     if (uc == 62)
 28     {
 29         return '+';
 30     }
 31     return '/';
 32 }
 33 
 34 // base64的实现
 35 void EncodeBase64(char *dbuf, char *buf128, int len)
 36 {
 37     struct Base64Date6 *ddd = NULL;
 38     int           i = 0;
 39     char          buf[256] = { 0 };
 40     char          *tmp = NULL;
 41     char          cc = '\0';
 42 
 43     memset(buf, 0, 256);
 44     strcpy(buf,buf128);
 45     for (i = 1; i <= len / 3; i++)
 46     {
 47         tmp = buf + (i - 1) * 3;
 48         cc = tmp[2];
 49         tmp[2] = tmp[0];
 50         tmp[0] = cc;
 51         ddd = (struct Base64Date6 *)tmp;
 52         dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1);
 53         dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2);
 54         dbuf[(i - 1) * 4 + 2] = ConvertToBase64((unsigned int)ddd->d3);
 55         dbuf[(i - 1) * 4 + 3] = ConvertToBase64((unsigned int)ddd->d4);
 56     }
 57     if (len % 3 == 1)
 58     {
 59         tmp = buf + (i - 1) * 3;
 60         cc = tmp[2];
 61         tmp[2] = tmp[0];
 62         tmp[0] = cc;
 63         ddd = (struct Base64Date6 *)tmp;
 64         dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1);
 65         dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2);
 66         dbuf[(i - 1) * 4 + 2] = '=';
 67         dbuf[(i - 1) * 4 + 3] = '=';
 68     }
 69     if (len % 3 == 2)
 70     {
 71         tmp = buf + (i - 1) * 3;
 72         cc = tmp[2];
 73         tmp[2] = tmp[0];
 74         tmp[0] = cc;
 75         ddd = (struct Base64Date6 *)tmp;
 76         dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1);
 77         dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2);
 78         dbuf[(i - 1) * 4 + 2] = ConvertToBase64((unsigned int)ddd->d3);
 79         dbuf[(i - 1) * 4 + 3] = '=';
 80     }
 81     return;
 82 }
 83 // 发送邮件
 84 void SendMail(char *email, const char *body)
 85 {
 86     int     sockfd = { 0 };
 87     char    buf[1500] = { 0 };
 88     char    rbuf[1500] = { 0 };
 89     char    login[128] = { 0 };
 90     char    pass[128] = { 0 };
 91     WSADATA WSAData;
 92     struct sockaddr_in their_addr = { 0 };
 93     WSAStartup(MAKEWORD(2, 2), &WSAData);
 94     memset(&their_addr, 0, sizeof(their_addr));
 95 
 96     their_addr.sin_family = AF_INET;
 97     their_addr.sin_port = htons(25);    // 一般是25端口不需要改
 98     hostent* hptr = gethostbyname("smtp.163.com");          // 端口和服务器
 99     memcpy(&their_addr.sin_addr.S_un.S_addr, hptr->h_addr_list[0], hptr->h_length);
100     printf("IP of smtp.163.com is : %d:%d:%d:%d\n",
101         their_addr.sin_addr.S_un.S_un_b.s_b1,
102         their_addr.sin_addr.S_un.S_un_b.s_b2,
103         their_addr.sin_addr.S_un.S_un_b.s_b3,
104         their_addr.sin_addr.S_un.S_un_b.s_b4);
105 
106     // 连接邮件服务器,如果连接后没有响应,则2 秒后重新连接
107     sockfd = OpenSocket((struct sockaddr *)&their_addr);
108     memset(rbuf, 0, 1500);
109     while (recv(sockfd, rbuf, 1500, 0) == 0)
110     {
111         cout << "reconnect..." << endl;
112         Sleep(2);
113         sockfd = OpenSocket((struct sockaddr *)&their_addr);
114         memset(rbuf, 0, 1500);
115     }
116 
117     cout << rbuf << endl;
118 
119     // EHLO
120     memset(buf, 0, 1500);
121     sprintf(buf,"%s", "EHLO HYL-PC\r\n");
122     send(sockfd, buf, strlen(buf), 0);
123     memset(rbuf, 0, 1500);
124     recv(sockfd, rbuf, 1500, 0);
125     cout << "EHLO REceive: " << rbuf << endl;
126 
127     // AUTH LOGIN
128     memset(buf, 0, 1500);
129     sprintf(buf, "%s", "AUTH LOGIN\r\n");
130     send(sockfd, buf, strlen(buf), 0);
131     memset(rbuf, 0, 1500);
132     recv(sockfd, rbuf, 1500, 0);
133     cout << "Auth Login Receive: " << rbuf << endl;
134 
135     // USER
136     memset(buf, 0, 1500);
137     sprintf(buf,"%s", "xxxxxxx@163.com");//你的邮箱账号
138     memset(login, 0, 128);
139     EncodeBase64(login, buf, strlen(buf));
140     sprintf(buf,"%s\r\n", login);
141     send(sockfd, buf, strlen(buf), 0);
142     cout << "Base64 UserName: " << buf << endl;
143     memset(rbuf, 0, 1500);
144     recv(sockfd, rbuf, 1500, 0);
145     cout << "User Login Receive: " << rbuf << endl;
146 
147     // PASSWORD
148     sprintf(buf,"%s", "xxxxxx");//你的邮箱密码
149     memset(pass, 0, 128);
150     EncodeBase64(pass, buf, strlen(buf));
151     sprintf(buf,"%s\r\n", pass);
152     send(sockfd, buf, strlen(buf), 0);
153     cout << "Base64 Password: " << buf << endl;
154 
155     memset(rbuf, 0, 1500);
156     recv(sockfd, rbuf, 1500, 0);
157     cout << "Send Password Receive: " << rbuf << endl;
158 
159     // MAIL FROM
160     memset(buf, 0, 1500);
161     sprintf(buf,"%s", "MAIL FROM: <xxxxxxxx@163.com>\r\n");  //此处要和发邮件的邮箱保持一致
162     send(sockfd, buf, strlen(buf), 0);
163     memset(rbuf, 0, 1500);
164     recv(sockfd, rbuf, 1500, 0);
165     cout << "set Mail From Receive: " << rbuf << endl;
166 
167     // RCPT TO 第一个收件人
168     sprintf(buf, "RCPT TO:<%s>\r\n", email);
169     send(sockfd, buf, strlen(buf), 0);
170     memset(rbuf, 0, 1500);
171     recv(sockfd, rbuf, 1500, 0);
172     cout << "Tell Sendto Receive: " << rbuf << endl;
173 
174     // DATA 准备开始发送邮件内容
175     sprintf(buf, "%s", "DATA\r\n");
176     send(sockfd, buf, strlen(buf), 0);
177     memset(rbuf, 0, 1500);
178     recv(sockfd, rbuf, 1500, 0);
179     cout << "Send Mail Prepare Receive: " << rbuf << endl;
180 
181     // 发送邮件内容,\r\n.\r\n内容结束标记
182     sprintf(buf,"%s\r\n.\r\n", body);
183     send(sockfd, buf, strlen(buf), 0);
184     memset(rbuf, 0, 1500);
185     recv(sockfd, rbuf, 1500, 0);
186     cout << "Send Mail Receive: " << rbuf << endl;
187 
188     // QUIT
189     sprintf(buf,"%s", "QUIT\r\n");
190     send(sockfd, buf, strlen(buf), 0);
191     memset(rbuf, 0, 1500);
192     recv(sockfd, rbuf, 1500, 0);
193     cout << "Quit Receive: " << rbuf << endl;
194 
195     //清理工作
196     closesocket(sockfd);
197     WSACleanup();
198     return;
199 }
200 // 打开TCP Socket连接
201 int OpenSocket(struct sockaddr *addr)
202 {
203     int sockfd = 0;
204     sockfd = socket(PF_INET, SOCK_STREAM, 0);
205     if (sockfd < 0)
206     {
207         cout << "Open sockfd(TCP) error!" << endl;
208         exit(-1);
209     }
210     if (connect(sockfd, addr, sizeof(struct sockaddr)) < 0)
211     {
212         cout << "Connect sockfd(TCP) error!" << endl;
213         exit(-1);
214     }
215     return sockfd;
216 }

SendMail.h

 1 #ifndef SENDMAIL_H_INCLUDED
 2 #define SENDMAIL_H_INCLUDED
 3 
 4 #include <windows.h>
 5 #include <stdio.h>
 6 #include <WinSock.h>
 7 #include <iostream>
 8 using namespace std;
 9 
10 // 协议中加密部分使用的是base64方法
11 char ConvertToBase64(char c6);
12 void EncodeBase64(char *dbuf, char *buf128, int len);
13 void SendMail(char *email, const char *body);
14 int  OpenSocket(struct sockaddr *addr);
15 
16 #endif // SENDMAIL_H_INCLUDED

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!