欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++發(fā)郵件簡單實例詳解

 更新時間:2018年05月28日 09:12:12   作者:ims  
這篇文章主要為大家詳細介紹了C++發(fā)郵件的簡單實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

C++發(fā)郵件用的是阻塞式socket模型,發(fā)送完數(shù)據(jù)后需要接收返回值,才能接著發(fā)送。

本程序不發(fā)送郵件附件,發(fā)附件的實例:C++實現(xiàn)含附件的郵件發(fā)送功能

#include <iostream>  
#include <string>  
#include <WinSock2.h> //適用平臺 Windows 
using namespace std; 
#pragma comment(lib, "ws2_32.lib") /*鏈接ws2_32.lib動態(tài)鏈接庫*/  
 
int main() 
{ 
 char buff[500]; //recv函數(shù)返回的結(jié)果 
 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"); //得到有關(guān)于域名的信息 
 SOCKADDR_IN addrServer; //服務(wù)端地址 
 addrServer.sin_addr.S_un.S_addr = *((DWORD *)pHostent->h_addr_list[0]); //得到smtp服務(wù)器的網(wǎng)絡(luò)字節(jié)序的ip地址   
 addrServer.sin_family = AF_INET; 
 addrServer.sin_port = htons(25); //連接端口25  
 //int connect (SOCKET s , const struct sockaddr FAR *name , int namelen ); //函數(shù)原型 
 err = connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR)); //向服務(wù)器發(fā)送請求  
 cout << "connect:" << err << endl; 
 buff[recv(sockClient, buff, 500, 0)] = '\0'; 
 cout << "connect:" << buff << endl; 
 /* 
 登錄郵件服務(wù)器 
 */ 
 message = "ehlo 126.com\r\n"; 
 send(sockClient, message.c_str(), message.length(), 0); //發(fā)送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; 
 /* 
 發(fā)送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 命令指定發(fā)送者 
 使用 RCPT 命令指定接收者,可以重復(fù)使用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 命令告訴服務(wù)器要發(fā)送郵件內(nèi)容 
 */ 
 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關(guān)鍵字與正文之間要有一個空行 
 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加密

可以用這個網(wǎng)站在線加密,工具地址

dos中登陸smtp服務(wù)器的命令

126郵箱:telnet smtp.126.com 25

qq郵箱:telnet smtp.qq.com 25

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論