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

边栏推荐
- 使用Karmada实现Helm应用的跨集群部署【云原生开源】
- Oracle trigger stored procedure writes at the same time
- 手机影像内卷几时休?
- Advantages and disadvantages of distributed file storage system
- 分布式文件存储系统的优点和缺点
- dns备用服务器信息,dns服务器地址(dns首选和备用填多少)
- 2021 CSP J2 entry group csp-s2 improvement group round 2 video and question solution
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- 多线程实现 重写run(),怎么注入使用mapper文件操作数据库
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行center中心化(每个数据列减去平均值)、设置method参数为center
猜你喜欢

es 根据索引名称和索引字段更新值

User authentication technology

Tdengine invitation: be a superhero who uses technology to change the world and become TD hero

【报名】基础架构设计:从架构热点问题到行业变迁 | TF63

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

小哥凭“量子速读”绝技吸粉59万:看街景图0.1秒,“啪的一下”在世界地图精准找到!...
![[registration] infrastructure design: from architecture hot issues to industry changes | tf63](/img/75/b83aaf9610987f695eefe350f8170e.jpg)
[registration] infrastructure design: from architecture hot issues to industry changes | tf63

通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑

基于STM32设计的蓝牙健康管理设备

谷歌浏览器 chropath插件
随机推荐
R语言plotly可视化:plotly可视化二维直方图等高线图、在等高线上添加数值标签、自定义标签字体色彩、设置鼠标悬浮显示效果(Styled 2D Histogram Contour)
【SO官方采访】为何使用Rust的开发者如此深爱它
[cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
通俗易懂理解朴素贝叶斯分类的拉普拉斯平滑
10 常见网站安全攻击手段及防御方法
Arduino PROGMEM静态存储区的使用介绍
Brother sucks 590000 fans with his unique "quantum speed reading" skill: look at the street view for 0.1 seconds, and "snap" can be accurately found on the world map
C语言学习-Day_04
【HCIE-RS复习思维导图】- STP
Mongodb cross host database copy and common commands
Technology is as important as business. It is wrong to favor either side
JS array splicing "suggested collection"
使用Karmada实现Helm应用的跨集群部署【云原生开源】
浅析基于边缘计算的移动AR实现(中)
torch.utils.data.RandomSampler和torch.utils.data.SequentialSampler的区别
[200 opencv routines] 212 Draw a slanted rectangle
For a moment, the ban of the US e-cigarette giant has been postponed, and products can be sold in the US for the time being
C語言學習-Day_04
Curiosity mechanism in reinforcement learning
Oracle trigger stored procedure writes at the same time