当前位置:网站首页>邮件系统(基于SMTP协议和POP3协议-C语言实现)
邮件系统(基于SMTP协议和POP3协议-C语言实现)
2022-06-27 09:38:00 【叶绿体不忘呼吸】
微信公众号:创享日记
发送关键词:邮件系统
获取邮件发送端和接收端C语言实现源代码源文件
1.邮件发送客户端详细设计
首先将必要信息填写完整,然后调用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("[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.邮件接收客户端详细设计
首先将必要信息填写完整,然后调用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 = "[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);
边栏推荐
- The R language uses the preprocess function of the caret package for data preprocessing: Center all data columns (subtract the average value from each data column), and set the method parameter to cen
- Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
- 有关二叉树的一些练习题
- 12 necessary tools for network engineers
- Google browser chropath plug-in
- E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
- 6月23日《Rust唠嗑室》第三期B站视频地址
- Es update values based on Index Names and index fields
- [system design] proximity service
猜你喜欢
NoSQL database redis installation
Quick start CherryPy (1)
Source insight 工具安装及使用方法
新旧两个界面对比
Process 0, process 1, process 2
SVN版本控制器的安装及使用方法
The markdown plug-in of the browser cannot display the picture
E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
Installation and use of SVN version controller
手把手带你玩摄像头模组
随机推荐
多個類的設計
6月23日《Rust唠嗑室》第三期B站视频地址
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
【OpenCV 例程200篇】212. 绘制倾斜的矩形
如何获取GC(垃圾回收器)的STW(暂停)时间?
详解各种光学仪器成像原理
R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
谷歌浏览器 chropath插件
ucore lab4
Tdengine invitation: be a superhero who uses technology to change the world and become TD hero
Only one ConfirmCallback is supported by each RabbitTemplate 解决办法
Some exercises about binary tree
How do I get the STW (pause) time of a GC (garbage collector)?
12 necessary tools for network engineers
MySQL proficient-01 addition, deletion and modification
ucore lab4
BufferedWriter 和 BufferedReader 的使用
更改pip镜像源
leetcode:522. 最长特殊序列 II【贪心 + 子序列判断】
【系统设计】邻近服务