国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++發郵件簡單實例詳解

2020-01-26 13:42:09
字體:
來源:轉載
供稿:網友

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武威市| 松桃| 双江| 桑植县| 烟台市| 关岭| 柞水县| 阿荣旗| 高雄县| 宣汉县| 乌拉特前旗| 广州市| 丹寨县| 福泉市| 桂阳县| 炉霍县| 神池县| 太仆寺旗| 衡水市| 谢通门县| 茌平县| 辽中县| 茶陵县| 凉城县| 金堂县| 彭水| 蓬莱市| 莲花县| 汕尾市| 长兴县| 郁南县| 普格县| 富蕴县| 台中县| 革吉县| 巴东县| 乌审旗| 唐河县| 吴桥县| 温泉县| 正宁县|