電子郵件客戶端程序設計與實現 c++

電子郵件客戶端程序設計與實現 C++實現


我在網上找了一些代碼發現都不行,一些是不能自定義標題和內容,有一些更直接,連發都發不出去,還有一些是報 "550 Connection denied."愣是停在了發出郵件的前夕。
我就去深入瞭解了用telnet發郵件的過程,然後我通過發給163郵箱反覆嘗試發現是你輸入郵件內容的格式問題,因爲格式不對qq郵箱發現郵件內容沒有收件人,發件人,主題,然後就自動給你攔截了,所以只要改對格式就能發給QQ郵箱的好友啦(順便說一句,163郵箱是真的啥郵件都能收,我這可不是吐槽他的安全問題啊) 經過修改終於寫出了既能自定義標題也能自定義收件人郵箱以及內容的代碼。

代碼如下:


#include <iostream>
#include <string>
#include <WinSock2.h> 
using namespace std;
#pragma comment(lib, "ws2_32.lib") 

int main()
{
   
     
     char buff[1500]; 
     string message;
     string info;
     string subject;

     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;//hostent是host entry的縮寫,該結構記錄主機的信息,包括主機名、別名、地址類型、地址長度和地址列表

     pHostent = gethostbyname("smtp.qq.com"); //得到有關於域名的信息,鏈接到qq郵箱服務器

     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, 1500, 0)] = '\0';
     cout << "connect:" << buff << endl;
     string ehlo = "ehlo 加上自己的QQ號或QQ郵箱地址\r\n";
     send(sockClient, ehlo.c_str(), ehlo.length(), 0);
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout <<"ehlo:"  << buff << endl;
     static string path = "auth login\r\n";
     send(sockClient, path.c_str(), path.length(), 0);
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout << "auth login:" << buff << endl;
     static string uername = "此處填base64加密的郵箱\r\n";
     send(sockClient, uername.c_str(), uername.length(), 0);
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout << "usrname:" << buff << endl;
     static string psw = "此處填base64加密的授權碼\r\n";
     send(sockClient, psw.c_str(), psw.length(), 0);
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout << "password:" << buff << endl;
     string mail;
     cout << "輸入收件人郵箱:";
     cin >> mail;
     message = "MAIL FROM:<此處填自己郵箱地址> \r\nRCPT TO:<";
     message.append(mail);
     message.append("> \r\n");
     cout << "message=" << message;
     send(sockClient, message.c_str(), message.length(), 0);
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout << "mail from的狀態碼: " << buff << endl;
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout << "rcpt to的狀態碼: " << buff << endl;
     message = "DATA\r\n";
     send(sockClient, message.c_str(), message.length(), 0);
     buff[recv(sockClient, buff, 1500, 0)] = '\0';
     cout<< "data命令返回的狀態碼: " << buff << endl;

     static string form= "from:<填自己郵箱地址>\r\nto:<"+mail+">\r\nsubject:";

        cout<<"主題:";
        cin>>subject;
        form.append(subject);
        form.append("\r\n\r\n");
        cout<<"內容:";
        cin>>info;
        form.append(info);
        form.append("\r\n.\r\n");
        send(sockClient, form.c_str(), form.length(), 0);
        message = "quit\r\n";
         send(sockClient, message.c_str(), message.length(), 0);
        buff[recv(sockClient, buff, 1500, 0)] = '\0';
         cout << "返回狀態碼:" << buff << endl;

         cout << "發送成功!"<<endl;
     system("pause");
}

telnet功能啓用

win10很多功能都默認關閉,要開啓相關功能才能實現,這裏把Telnet Client功能打上鉤即可

開啓Telnet Client功能

用telnet發送電子郵件

在cmd窗口輸入以下命令進入第三方登錄界面
啓用功能

連接成功後
在這裏插入圖片描述
用ehlo命令輸入自己的QQ號進行登錄
在這裏插入圖片描述


選擇auth login 方式登錄
在這裏插入圖片描述
返回334,成功
然後輸入自己base64加密的郵箱地址
在這裏插入圖片描述
返回334,成功
再輸入自己base64加密的授權碼
在這裏插入圖片描述
返回235,成功登錄。
輸入發件地址和收件地址,報502多試幾次就行了。








在這裏插入圖片描述

用data命令輸入要傳送的數據
在這裏插入圖片描述
數據的格式,以回車鍵加一個點"."再加回車鍵結束
在這裏插入圖片描述
qq郵箱收到的郵件
在這裏插入圖片描述




代碼實現:
在這裏插入圖片描述
QQ郵箱收到的郵件
在這裏插入圖片描述


然後就ok了



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章