当前位置:网站首页>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

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);

边栏推荐
- Ubuntu手動安裝MySQL
- Product strength benchmarking seal /model 3, with 179800 pre-sales of Chang'an dark blue sl03
- 10 常见网站安全攻击手段及防御方法
- 2021 CSP J2入门组 CSP-S2提高组 第2轮 视频与题解
- leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】
- 【OpenCV 例程200篇】211. 绘制垂直矩形
- 12个网络工程师必备工具
- dns备用服务器信息,dns服务器地址(dns首选和备用填多少)
- Advantages and disadvantages of distributed file storage system
- 用户认证技术
猜你喜欢
随机推荐
Leetcode to do questions
[registration] infrastructure design: from architecture hot issues to industry changes | tf63
[cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
以后发现漏洞,禁止告诉中国!
Use of bufferedwriter and BufferedReader
Arduino PROGMEM静态存储区的使用介绍
Bluetooth health management device based on stm32
一次线上移动端报表网络连接失败问题定位与解决
torchvision.models._utils.IntermediateLayerGetter使用教程
Location and solution of network connection failure of primary online mobile terminal Report
[从零开始学习FPGA编程-47]:视野篇 - 第三代半导体技术现状与发展趋势
Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
谷歌浏览器 chropath插件
C language learning day_ 06
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.1 生成树协议(Spanning Tree Protocol (STP))
unity--newtonsoft.json解析
【SO官方采访】为何使用Rust的开发者如此深爱它
Memory compression for win10
R language uses econcharts package to create microeconomic or macro-economic charts, demand function to visualize demand curve, and customize the parameters of demand function to enrich the visualizat
使用Karmada实现Helm应用的跨集群部署【云原生开源】









