C++发送邮件实现代码
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
本文实例为大家分享了C++发送邮件的具体代码,供大家参考,具体内容如下
首先,别忘了要设置发送邮箱的smtp,例如,假设你需要用网易邮箱,你需要去你的163邮箱设置开启smtp(有的邮箱还需要设置授权码)。接着就可以用以下代码发送邮件了:
// SendMail.h #ifndef _SEND_MAIL_H_ #define _SEND_MAIL_H_ #include <windows.h> #include <stdio.h> #include <WinSock.h> #include <iostream> using namespace std; // 协议中加密部分使用的是base64方法 char ConvertToBase64(char c6); void EncodeBase64(char *dbuf, char *buf128, int len); void SendMail(char *email, const char *body); int OpenSocket(struct sockaddr *addr); #endif
// SendMail.cpp #include "SendMail.h" #pragma comment(lib, "ws2_32.lib") struct Base64Date6 { unsigned int d4 : 6; unsigned int d3 : 6; unsigned int d2 : 6; unsigned int d1 : 6; }; char ConvertToBase64(char uc) { if (uc < 26) { return 'A' + uc; } if (uc < 52) { return 'a' + (uc - 26); } if (uc < 62) { return '0' + (uc - 52); } if (uc == 62) { return '+'; } return '/'; } // base64的实现 void EncodeBase64(char *dbuf, char *buf128, int len) { struct Base64Date6 *ddd = NULL; int i = 0; char buf[256] = { 0 }; char *tmp = NULL; char cc = '\0'; memset(buf, 0, 256); strcpy_s(buf, 256, buf128); for (i = 1; i <= len / 3; i++) { tmp = buf + (i - 1) * 3; cc = tmp[2]; tmp[2] = tmp[0]; tmp[0] = cc; ddd = (struct Base64Date6 *)tmp; dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1); dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2); dbuf[(i - 1) * 4 + 2] = ConvertToBase64((unsigned int)ddd->d3); dbuf[(i - 1) * 4 + 3] = ConvertToBase64((unsigned int)ddd->d4); } if (len % 3 == 1) { tmp = buf + (i - 1) * 3; cc = tmp[2]; tmp[2] = tmp[0]; tmp[0] = cc; ddd = (struct Base64Date6 *)tmp; dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1); dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2); dbuf[(i - 1) * 4 + 2] = '='; dbuf[(i - 1) * 4 + 3] = '='; } if (len % 3 == 2) { tmp = buf + (i - 1) * 3; cc = tmp[2]; tmp[2] = tmp[0]; tmp[0] = cc; ddd = (struct Base64Date6 *)tmp; dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1); dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2); dbuf[(i - 1) * 4 + 2] = ConvertToBase64((unsigned int)ddd->d3); dbuf[(i - 1) * 4 + 3] = '='; } return; } // 发送邮件 void SendMail(char *email, const char *body) { int sockfd = { 0 }; char buf[1500] = { 0 }; char rbuf[1500] = { 0 }; char login[128] = { 0 }; char pass[128] = { 0 }; WSADATA WSAData; struct sockaddr_in their_addr = { 0 }; WSAStartup(MAKEWORD(2, 2), &WSAData); memset(&their_addr, 0, sizeof(their_addr)); their_addr.sin_family = AF_INET; their_addr.sin_port = htons(25); // 一般是25端口不需要改 hostent* hptr = gethostbyname("smtp.126.com"); // 端口和服务器 memcpy(&their_addr.sin_addr.S_un.S_addr, hptr->h_addr_list[0], hptr->h_length); printf("IP of smpt.163.com is : %d:%d:%d:%d\n", their_addr.sin_addr.S_un.S_un_b.s_b1, their_addr.sin_addr.S_un.S_un_b.s_b2, their_addr.sin_addr.S_un.S_un_b.s_b3, their_addr.sin_addr.S_un.S_un_b.s_b4); // 连接邮件服务器,如果连接后没有响应,则2 秒后重新连接 sockfd = OpenSocket((struct sockaddr *)&their_addr); memset(rbuf, 0, 1500); while (recv(sockfd, rbuf, 1500, 0) == 0) { cout << "reconnect..." << endl; Sleep(2); sockfd = OpenSocket((struct sockaddr *)&their_addr); memset(rbuf, 0, 1500); } cout << rbuf << endl; // EHLO memset(buf, 0, 1500); sprintf_s(buf, 1500, "EHLO HYL-PC\r\n"); send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "EHLO REceive: " << rbuf << endl; // AUTH LOGIN memset(buf, 0, 1500); sprintf_s(buf, 1500, "AUTH LOGIN\r\n"); send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "Auth Login Receive: " << rbuf << endl; // USER memset(buf, 0, 1500); sprintf_s(buf, 1500, "xxxxxx@126.com");//你的邮箱账号 memset(login, 0, 128); EncodeBase64(login, buf, strlen(buf)); sprintf_s(buf, 1500, "%s\r\n", login); send(sockfd, buf, strlen(buf), 0); cout << "Base64 UserName: " << buf << endl; memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "User Login Receive: " << rbuf << endl; // PASSWORD sprintf_s(buf, 1500, "********");//你的邮箱密码 memset(pass, 0, 128); EncodeBase64(pass, buf, strlen(buf)); sprintf_s(buf, 1500, "%s\r\n", pass); send(sockfd, buf, strlen(buf), 0); cout << "Base64 Password: " << buf << endl; memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "Send Password Receive: " << rbuf << endl; // MAIL FROM memset(buf, 0, 1500); sprintf_s(buf, 1500, "MAIL FROM: <xxxx@126.com>\r\n"); //此处要和发邮件的邮箱保持一致 send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "set Mail From Receive: " << rbuf << endl; // RCPT TO 第一个收件人 sprintf_s(buf, 1500, "RCPT TO:<%s>\r\n", email); send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "Tell Sendto Receive: " << rbuf << endl; // DATA 准备开始发送邮件内容 sprintf_s(buf, 1500, "DATA\r\n"); send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "Send Mail Prepare Receive: " << rbuf << endl; // 发送邮件内容,\r\n.\r\n内容结束标记 sprintf_s(buf, 1500, "%s\r\n.\r\n", body); send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "Send Mail Receive: " << rbuf << endl; // QUIT sprintf_s(buf, 1500, "QUIT\r\n"); send(sockfd, buf, strlen(buf), 0); memset(rbuf, 0, 1500); recv(sockfd, rbuf, 1500, 0); cout << "Quit Receive: " << rbuf << endl; //清理工作 closesocket(sockfd); WSACleanup(); return; } // 打开TCP Socket连接 int OpenSocket(struct sockaddr *addr) { int sockfd = 0; sockfd = socket(PF_INET, SOCK_STREAM, 0); if (sockfd < 0) { cout << "Open sockfd(TCP) error!" << endl; exit(-1); } if (connect(sockfd, addr, sizeof(struct sockaddr)) < 0) { cout << "Connect sockfd(TCP) error!" << endl; exit(-1); } return sockfd; }
测试例程:
// SendMailTest.cpp #include "SendMail.h" int main() { int num = 100; string EmailContents = "From: \"lucy\"<xxxx@126.com>\r\n" + string("To: \"dasiy\"<yyyy@126.com>\r\n") + "Subject: Hello\r\n\r\n" + "test sending variable" + to_string(num) + "\n"; char EmailTo[] = "yyyy@126.com"; //此处是送达的邮箱, 需和EmailContents里的保持一致 SendMail(EmailTo, EmailContents.c_str()); return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 04-02c语言没有round函数 round c语言
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10c++中inline的用法分析
- 01-10用C++实现DBSCAN聚类算法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10C++大数模板(推荐)
- 01-10浅谈C/C++中的static与extern关键字的使用详解
- 01-10深入C/C++浮点数在内存中的存储方式详解
- 01-10深入理解C/C++混合编程
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10C#中split用法实例总结
- 01-11ajax实现页面的局部加载
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10delphi制作wav文件的方法
- 08-05织梦dedecms什么时候用栏目交叉功能?