電子郵件客戶端程式設計與實作 C++實作
- telnet功能啟用
- 用telnet發送電子郵件
我在網上找了一些代碼發現都不行,一些是不能自定義標題和內容,有一些更直接,連發都發不出去,還有一些是報 "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發送電子郵件
在cmd視窗輸入以下命令進入第三方登錄界面

連接成功后

用ehlo命令輸入自己的QQ號進行登錄

選擇auth login 方式登錄

回傳334,成功
然后輸入自己base64加密的郵箱地址

回傳334,成功
再輸入自己base64加密的授權碼

回傳235,成功登錄,
輸入發件地址和收件地址,報502多試幾次就行了,

用data命令輸入要傳送的資料

資料的格式,以回車鍵加一個點"."再加回車鍵結束

qq郵箱收到的郵件

代碼實作:

QQ郵箱收到的郵件

然后就ok了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/245793.html
標籤:其他
