栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

微信公众号:创享日记
发送关键词:邮件系统
获取邮件发送端和接收端C语言实现源代码源文件



1.邮件发送客户端详细设计
首先将必要信息填写完整,然后调用socket ()函数创建一个socket并获得其文件描述符,然后定义并填写一个sockaddr_ in结构体作为后面connect ()函数的参数,接着调用connect函数来建立一个TCP连接;然后发送EHLO命令并打印出服务器的回复,然后是发送AUTH命令(AUTH login) 并打印服务器回复,接着发送用户名以及在邮箱中的得到的授权码并且分别打印服务器回复;接着发送邮件发送者的邮箱地址以及邮件接收者的邮箱地址并分别打印服务器回复;发送DATA命令(用于输入邮件内容,该命令后面发送的所有数据都将被当做邮件内容,直至遇到结束标志字符串)并打印服务器回复;接着开始发送邮件内容,依次发送邮件发送者信息、邮件接收者信息、正文、附件等信息,其中正文以及附件采用文件读写的方式,从文件中读出相应的信息再发送给服务器;最后发送QUIT命令并打印服务器回复。

send部分代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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 = "rn.rn";
    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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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] = ''; // Do not forget the null terminator
    printf("%s", buf);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号