參照教材 6.5 節原理,設計一個電子郵件客戶端程式,
可實作發送與接受,
如果發送郵件出去接受不到請把代碼中的輸出注釋取消,運行確定錯誤地點
不懂的評論問
base64在線轉碼:
https://base64.us/
#include <iostream>
#include <string>
#include <WinSock2.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib") /*鏈接ws2_32.lib元件*/
int main() {
char buff[50000]; //收到recv函式回傳的結果
string message;
string info;
string subject;
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 1);
//WSAStarup,即WSA(Windows SocKNDs Asynchronous,Windows套接字異步)的啟動命令
int err = WSAStartup(wVersionRequested, &wsaData);
SOCKADDR_IN addrServer; //服務端地址
HOSTENT *pHostent;//hostent是host entry的縮寫,該結構記錄主機的資訊,包括主機名、別名、地址型別、地址長度和地址串列
SOCKET sockClient; //客戶端的套接字
/*
使用 MAIL 命令指定發送者
使用 RCPT 命令指定接收者,可以重復使用RCPT指定多個接收者
*/
cout << "你想查看郵件還是發郵件?\n\t1.查看郵箱\n\t2.發送郵件\n";
int call;
cin >> call;
if (call == 2) {
sockClient = socket(AF_INET, SOCK_STREAM, 0); //建立socket物件
pHostent = gethostbyname("smtp.qq.com"); //得到有關于域名的資訊,鏈接到qq郵箱服務器
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)); //向服務器發送請求
buff[recv(sockClient, buff, 500, 0)] = '\0';
/*
登錄郵件服務器
*/
message = "ehlo qq.com\r\n";
send(sockClient, message.c_str(), message.length(), 0); //發送ehlo命令
buff[recv(sockClient, buff, 500, 0)] = '\0'; //接識訓傳值
// cout <<"1" << buff << endl;
message = "auth login\r\n";
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"2" << buff << endl;
/*
發送base64加密的用戶名、密碼
*/
message = "MTc2NjQ2ODQzNA==\r\n";
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"3" << buff << endl;
message = "bmVmdHltemNpd2lxY2ZmaQ==\r\n";/**/
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"4" << buff << endl;
string mail;
cout << "請輸入收件人郵箱:";
cin >> mail;
message = "MAIL FROM:<1766468434@qq.com> \r\nRCPT TO:<";
message.append(mail);
message.append("> \r\n");
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"5" << buff << endl;
// buff[recv(sockClient, buff, 500, 0)] = '\0';
/*
使用 DATA 命令告訴服務器要發送郵件內容
*/
message = "DATA\r\n";
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"6" << buff << endl;
message = "From: 1766468434@qq.com\r\nTo: " + mail + "\r\nsubject:";
cout << "主題:";
cin >> subject;
message.append(subject);
message.append("\r\n\r\n");
cout << "內容:";
cin >> info;
message.append(info);
message.append("\r\n.\r\n");
send(sockClient, message.c_str(), message.length(), 0);
// cout <<"7" << buff << endl;
message = "QUIT\r\n";
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"8" << buff << endl;
cout << "發送成功!" << endl;
//system("pause");
}
if (call == 1) {
sockClient = socket(AF_INET, SOCK_STREAM, 0); //建立socket物件
const char *host_id = "pop3.126.com";
pHostent = gethostbyname("pop.qq.com");
int port = 110;
addrServer.sin_addr.S_un.S_addr = *((DWORD *) pHostent->h_addr_list[0]); //得到smtp服務器的網路位元組序的ip地址
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(port); //連接埠110
err = connect(sockClient, (SOCKADDR *) &addrServer, sizeof(SOCKADDR)); //向服務器發送請求
buff[recv(sockClient, buff, 500, 0)] = '\0';
message = "user 1766468434@qq.com\r\n";
send(sockClient, message.c_str(), message.length(), 0); //發送賬號
buff[recv(sockClient, buff, 500, 0)] = '\0'; //接識訓傳值
std::cout << "Client : send name \nServer:"
<< buff << std::endl;
message = "pass neftymzciwiqcffi\r\n";
send(sockClient, message.c_str(), message.length(), 0); //發送Mima
buff[recv(sockClient, buff, 500, 0)] = '\0'; //接識訓傳值
std::cout << "Client : send pass \nServer:"
<< buff << std::endl;
message = "stat\r\n";
send(sockClient, message.c_str(), message.length(), 0); //發送狀態
buff[recv(sockClient, buff, 500, 0)] = '\0'; //接識訓傳值
sleep(1);
std::cout << "Client : send stat \nServer : "
<< buff << std::endl;
message = "list\r\n";
send(sockClient, message.c_str(), message.length(), 0); //發送狀態
buff[recv(sockClient, buff, 50000, 0)] = '\0'; //接識訓傳值
sleep(1);
std::cout << "Client : send list \nServer :"
<< buff << std::endl;
int n;
std::cout << "你先想查看那一封郵件?輸入序號"
<< std::endl;
cin >> n;
message = "retr " + to_string(n) + "\r\n";
send(sockClient, message.c_str(), message.length(), 0); //發送狀態
sleep(1);
std::cout << "Client : send retr (...) \n";
//下面的while回圈有些問題,目前還沒有想到解決方法,以后改正!
// while(1)
// {
buff[recv(sockClient, buff, 50000, 0)] = '\0'; //接識訓傳值
std::cout << "Server :" << buff << std::endl;
/*int num = sock.recv_socket();
std::cout << "Server :" << sock.get_recvbuf() << std::endl;*/
/* if(num <= 0)
{
break;
}*/
// }
/* sock.send_socket("quit\r\n");
std::cout << "Client : send quit \nServer :"
<< sock.get_recvbuf() << std::endl;}*/
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/243341.html
標籤:其他
上一篇:Springboot+shiro+mybatis-plus+vue前后端分離專案設計架構
下一篇:golang切片擴容規則
