C++發郵件用的是阻塞式socket模型,發送完數據后需要接收返回值,才能接著發送。
本程序不發送郵件附件,發附件的實例:C++實現含附件的郵件發送功能
#include <iostream> #include <string> #include <WinSock2.h> //適用平臺 Windows using namespace std; #pragma comment(lib, "ws2_32.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; /* 發送base64加密的用戶名、密碼 */ 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; /* 使用 MAIL 命令指定發送者 使用 RCPT 命令指定接收者,可以重復使用RCPT指定多個接收者 */ 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; /* 使用 DATA 命令告訴服務器要發送郵件內容 */ 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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。