当前位置:网站首页>Mail system (based on SMTP protocol and POP3 protocol -c language implementation)

Mail system (based on SMTP protocol and POP3 protocol -c language implementation)

2022-06-27 10:15:00 Chloroplasts do not forget to breathe

WeChat official account : Chuangxiang diary
Send keywords : Mail system
Get the sender and receiver of the mail C Language implementation source code source file


 Insert picture description here


1. Mail sending client detailed design
First fill in the necessary information , And then call socket () Function to create a socket And get its file descriptor , Then define and fill in a sockaddr_ in The structure acts as the back connect () The parameters of the function , Then call connect Function to create a TCP Connect ; And then send EHLO Command and print out the server's reply , Then send AUTH command (AUTH login) And print the server reply , Then send the user name and the authorization code in the mailbox and print the server reply respectively ; Then send the email address of the mail sender and the email address of the mail receiver, and print the server reply respectively ; send out DATA command ( Used to enter the contents of the message , All data sent after this command will be treated as the contents of the email , Until the end flag string is encountered ) And print the server reply ; Then start sending email content , Send mail sender information in turn 、 Message recipient information 、 Text 、 Attachment and other information , The text and attachments are read and written in the form of documents , Read out the corresponding information from the file and send it to the server ; Last send QUIT Command and print the server reply .

send Part of the code :

#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("[email protected]"); // TODO: Specify the user
    const char* pass = encode_str("rvamphcwfujphffj"); // TODO: Specify the password
    const char* from = "[email protected]"; // 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. Detailed design of mail receiving client
First fill in the necessary information , And then call socket () Function to create a socket And get its file descriptor , Then define and fill in a sockaddr_ _in The structure acts as the back connect () The parameters of the function , Then call connect Function to create a TCP Connect ; Then send the user name and authorization code and print the server reply respectively ; Then send in sequence STAT、LIST、 RETR 1、QUIT Command and print the server reply separately .

recv Part of the code :

#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 = "[email protected]"; // 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);

 Insert picture description here

原网站

版权声明
本文为[Chloroplasts do not forget to breathe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270937504975.html