1.邮件发送客户端详细设计
首先将必要信息填写完整,然后调用socket ()函数创建一个socket并获得其文件描述符,然后定义并填写一个sockaddr_ in结构体作为后面connect ()函数的参数,接着调用connect函数来建立一个TCP连接;然后发送EHLO命令并打印出服务器的回复,然后是发送AUTH命令(AUTH login) 并打印服务器回复,接着发送用户名以及在邮箱中的得到的授权码并且分别打印服务器回复;接着发送邮件发送者的邮箱地址以及邮件接收者的邮箱地址并分别打印服务器回复;发送DATA命令(用于输入邮件内容,该命令后面发送的所有数据都将被当做邮件内容,直至遇到结束标志字符串)并打印服务器回复;接着开始发送邮件内容,依次发送邮件发送者信息、邮件接收者信息、正文、附件等信息,其中正文以及附件采用文件读写的方式,从文件中读出相应的信息再发送给服务器;最后发送QUIT命令并打印服务器回复。
send部分代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #include <getopt.h> #include "base64_utils.h" #define MAX_SIZE 4095 char buf[MAX_SIZE+1]; // receiver: mail address of the recipient // subject: mail subject // msg: content of mail body or path to the file containing mail body // att_path: path to the attachment void send_mail(const char* receiver, const char* subject, const char* msg, const char* att_path) { const char* end_msg = "\r\n.\r\n"; const char* host_name = "smtp.qq.com"; // TODO: Specify the mail server domain name const unsigned short port = 25; // SMTP server port const char* user = encode_str("1197879738@qq.com"); // TODO: Specify the user const char* pass = encode_str("rvamphcwfujphffj"); // TODO: Specify the password const char* from = "1197879738@qq.com"; // TODO: Specify the mail address of the sender char dest_ip[16]; // Mail server IP address int s_fd; // socket file descriptor struct hostent *host; struct in_addr **addr_list; int i = 0; int r_size; // Get IP from domain name if ((host = gethostbyname(host_name)) == NULL) { herror("gethostbyname"); exit(EXIT_FAILURE); } addr_list = (struct in_addr **) host->h_addr_list; while (addr_list[i] != NULL) ++i; strcpy(dest_ip, inet_ntoa(*addr_list[i-1]));
2.邮件接收客户端详细设计
首先将必要信息填写完整,然后调用socket ()函数创建一个socket 并获得其文件描述符,然后定义并填写一个sockaddr_ _in 结构体作为后面connect () 函数的参数,接着调用connect 函数来建立一个TCP连接;然后发送用户名和授权码并分别打印服务器回复;接着依次发送STAT、LIST、 RETR 1、QUIT命令并分别打印服务器回复。
recv部分代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #define MAX_SIZE 65535 char buf[MAX_SIZE+1]; void recv_mail() { const char* host_name = "pop.qq.com"; // TODO: Specify the mail server domain name const unsigned short port = 110; // POP3 server port const char* user = "1197879738@qq.com"; // TODO: Specify the user const char* pass = "rvamphcwfujphffj"; // TODO: Specify the password char dest_ip[16]; int s_fd; // socket file descriptor struct hostent *host; struct in_addr **addr_list; int i = 0; int r_size; // Get IP from domain name if ((host = gethostbyname(host_name)) == NULL) { herror("gethostbyname"); exit(EXIT_FAILURE); } addr_list = (struct in_addr **) host->h_addr_list; while (addr_list[i] != NULL) ++i; strcpy(dest_ip, inet_ntoa(*addr_list[i-1])); // TODO: Create a socket,return the file descriptor to s_fd, and establish a TCP connection to the POP3 server s_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; struct sockaddr_in *addr_in=&addr; addr.sin_family = AF_INET; addr.sin_port = (port << 8) | (port >> 8); addr_in->sin_addr.s_addr = inet_addr(dest_ip); connect(s_fd, addr_in, sizeof(addr)); // printf welcome message if ((r_size = recv(s_fd, buf, MAX_SIZE, 0)) == -1) { perror("recv"); exit(EXIT_FAILURE); } buf[r_size] = '\0'; // Do not forget the null terminator printf("%s", buf);