3

计算机网络课程设计-电子邮件客户端程序设计与实现

 2 years ago
source link: https://1905060202.github.io/2021/12/28/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1-%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
胡小宁的博客

计算机网络课程设计-电子邮件客户端程序设计与实现

发表于2021-12-28|更新于2021-12-28|计算机基础
阅读量:58

电子邮件客户端程序设计与实现

今天是课程设计的第二天,这次课程设计我只打算做五个实验,这是第二个。

首先要配置测试使用的QQ邮箱,不然会出现服务器响应为:Error: need EHLO and AUTH first !”问题。

配置qq邮箱的步骤及填写授权码

1、首先在QQ邮箱当中开启“POP3/SMTP服务”

2、开启后点击下方生成授权码

3、使用base64加密账号与密码

4、使用base64加密后的授权码替代登陆密码,如下图所示:

5.尝试调试程序

设计结果及结果分析

查看邮件:

发送邮件:

#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动态链接库*/

void mail(){
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 = "MTAyNTE0MjkzMg==\r\n";
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"3" << buff << endl;

message = "bXhkeXd1ZnVhcHVnYmVlag==\r\n";/**/
send(sockClient, message.c_str(), message.length(), 0);
buff[recv(sockClient, buff, 500, 0)] = '\0';
// cout <<"4" << buff << endl;

/*
Author:hyn
Time:2021.12.28
*/

string mail;
cout << "请输入收件人邮箱:";
cin >> mail;
//[email protected]
message = "MAIL FROM:<[email protected]> \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: [email protected]\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 [email protected]\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 mxdywufuapugbeej\r\n";
send(sockClient, message.c_str(), message.length(), 0); //发送授权码
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);
cout << "Client : send list \nServer :"
<< buff << endl;
while(1)
{
int n,option;
cout << "你先想查看那一封邮件?输入序号"
<< endl;
cin >> n;
message = "retr " + to_string(n) + "\r\n";

send(sockClient, message.c_str(), message.length(), 0); //发送状态
// sleep(1);
cout << "Client : send retr (...) \n";

buff[recv(sockClient, buff, 50000, 0)] = '\0'; //接收返回值
cout << "Server :" << buff << endl;

cout<<"1.继续查看 2.退出";
cin>>option;
if(option == 1)
;
else
break;
}
}
}

int main()
{
while(1){
mail();
}
}



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK