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)文章
淺談C/C++中的static與extern關(guān)鍵字的使用詳解
本篇文章是對C/C++中的static與extern關(guān)鍵字的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
通過stringstream實現(xiàn)常用的類型轉(zhuǎn)換實例代碼
在本篇文章里小編給大家分享了關(guān)于通過stringstream實現(xiàn)常用的類型轉(zhuǎn)換實例代碼內(nèi)容,需要的朋友們可以參考下。2020-04-04

