邮件系统(基于SMTP协议和POP3协议-C语言实现)

简介: 邮件系统(基于SMTP协议和POP3协议-C语言实现)

4a8c25cb80b44a4a92614be01407b321.png1.邮件发送客户端详细设计

首先将必要信息填写完整,然后调用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);

63ca26b8caa7468d979ed2c8ab1729e2.png



相关文章
|
9月前
|
C语言
C语言根据协议分割获取字符串单元
C语言根据协议分割获取字符串单元
43 0
|
4天前
|
存储 安全 网络协议
邮件协议揭秘:SMTP与IMAP的双重功能解析
SMTP和IMAP是电子邮件系统的核心协议,SMTP负责邮件发送,通过SSL/TLS保证安全,而IMAP则处理邮件接收和管理,支持服务器存储及状态同步。这两种协议相辅相成,为现代邮件系统提供了坚实基础。它们广泛应用于各种邮件客户端,确保了兼容性、功能丰富性和安全性,满足用户对电子邮件的多样化需求。
20 3
|
21天前
|
存储 网络协议 算法
电子邮件协议(SMTP,MIME,POP3,IMAP)
电子邮件协议(SMTP,MIME,POP3,IMAP)
36 1
|
21天前
带你了解SMTP,POP3,IMAP协议
带你了解SMTP,POP3,IMAP协议
|
8月前
|
网络协议 数据安全/隐私保护
百度搜索:蓝易云【网络通信协议-SMTP协议详解!】
总之,SMTP是一种用于电子邮件传输的协议,用于在网络中发送和路由邮件。它通过建立连接、握手、身份验证和邮件传输等步骤实现邮件的可靠传递。SMTP协议在电子邮件系统中起着重要的作用,使得邮件可以在不同的邮件服务器之间进行传递和投递。
182 0
|
8月前
|
存储
SMTP 和 POP3 协议的区别和联系
SMTP 和 POP3 协议的区别和联系
102 1
|
11月前
|
存储 网络协议 物联网
C语言代码封装MQTT协议报文,了解MQTT协议通信过程
MQTT是一种轻量级的通信协议,适用于物联网(IoT)和低带宽网络环境。它基于一种“发布/订阅”模式,其中设备发送数据(也称为 “发布”)到经纪人(称为MQTT代理),这些数据被存储,并在需要时被转发给订阅者。这种方式简化了网络管理,允许多个设备在不同的网络条件下进行通信(包括延迟和带宽限制),并支持实时数据更新。它是开放的,可免费使用并易于实施。
295 0
|
人工智能
邮件开发:一些常见邮箱的POP3及SMTP服务器地址
邮件开发:一些常见邮箱的POP3及SMTP服务器地址
281 0
|
网络安全 数据安全/隐私保护 Python
Python3使用SMTP协议发送邮件
Python3使用SMTP协议发送邮件
110 0
Python3使用SMTP协议发送邮件
|
C语言
C语言实现学生成绩管理系统
本文提供一例C语言实现的命令行学生信息管理系统,供初学者参考。
302 0