时间:2021-05-20
C++发邮件用的是阻塞式socket模型,发送完数据后需要接收返回值,才能接着发送。
本程序不发送邮件附件,发附件的实例:C++实现含附件的邮件发送功能
#include <iostream> #include <string> #include <WinSock2.h> //适用平台 Windows using namespace std; #pragma comment(lib, "ws2_32.lib") int main() { char buff[500]; //recv函数返回的结果 string message; WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 1); //WSAStarup,即WSA(Windows SocKNDs Asynchronous,Windows套接字异步)的启动命令 int err = WSAStartup(wVersionRequested, &wsaData); cout << "WSAStartup:" << err << endl; SOCKET sockClient; //客户端的套接字 sockClient = socket(AF_INET, SOCK_STREAM, 0); //建立socket对象 HOSTENT* pHostent; pHostent = gethostbyname("smtp.126.com"); //得到有关于域名的信息 SOCKADDR_IN addrServer; //服务端地址 addrServer.sin_addr.S_un.S_addr = *((DWORD *)pHostent->h_addr_list[0]); //得到smtp服务器的网络字节序的ip地址 addrServer.sin_family = AF_INET; addrServer.sin_port = htons(25); //连接端口25 //int connect (SOCKET s , const struct sockaddr FAR *name , int namelen ); //函数原型 err = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR)); //向服务器发送请求 cout << "connect:" << err << endl; buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "connect:" << buff << endl; message = "ehlo 126.com\r\n"; send(sockClient, message.c_str(), message.length(), 0); //发送ehlo命令 buff[recv(sockClient, buff, 500, 0)] = '\0'; //接收返回值 cout << "helo:" << buff << endl; //输出返回值 message = "auth login \r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "auth login:" << buff << endl; message = "xxxx\r\n"; //base64 编码的用户名 send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "usrname:" << buff << endl; message = "xxxx\r\n";//base64 编码的密码 send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "password:" << buff << endl; message = "MAIL FROM:<xxxx@126.com> \r\nRCPT TO:<xxxx@126.com> \r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "mail from: " << buff << endl; buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "rcpt to: " << buff << endl; message = "DATA\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "data: " << buff << endl; message = "From: Bob@example.com\r\n\ To: Alice@example.com\r\n\ Cc: theboss@example.com\r\n\ subject: subject\r\n\r\n\ Hello Alice\r\n\ This is a test message with 4 header fields and 4 lines in the message body\r\n\ your friend\r\n\ Bob\r\n.\r\n"; //注意subject关键字与正文之间要有一个空行 send(sockClient, message.c_str(), message.length(), 0); message = "QUIT\r\n"; send(sockClient, message.c_str(), message.length(), 0); buff[recv(sockClient, buff, 500, 0)] = '\0'; cout << "QUIT:" << buff << endl; system("pause"); }邮件效果图
Telnet做个对比
邮箱的用户名和密码用BASE64加密
可以用这个网站在线加密,工具地址
dos中登陆smtp服务器的命令
126邮箱:telnet smtp.126.com 25
qq邮箱:telnet smtp.qq.com 25
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C++中cerr和cout的区别实例详解前言:cerrTheobjectcontrolsunbufferedinsertionstothestandarderr
C++中字符串操作--宽窄字符转换的实例详解MultiByteToWideCharintMultiByteToWideChar(_In_UINTCodePage
C++类中的继承实例详解实例效果:实现代码:#include#includeusingnamespacestd;classPerson{public:Perso
C++中const修饰虚函数实例详解【1】程序1#includeusingnamespacestd;classBase{public:virtualvoidpr
C++实现邮件发送程序在vs2013测试通过,一共3个文件,发邮件的程序封装为Csmtp类。1.测试用的主函数//#include"Csmtp.h"#pragm