-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 89624c7
Showing
10 changed files
with
2,061 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from building import * | ||
|
||
# get current directory | ||
cwd = GetCurrentDir() | ||
# The set of source files associated with this SConscript file. | ||
src = Split(""" | ||
src/smtp_client_data.c | ||
src/smtp_client.c | ||
""") | ||
|
||
if GetDepend(['SMTP_CLIENT_USING_TLS']): | ||
src += Glob('src/smtp_client_tls.c') | ||
|
||
if GetDepend(['SMTP_CLIENT_USING_SAMPLE']): | ||
src += Glob('example/smtp_client_example.c') | ||
|
||
path = [cwd + '/inc'] | ||
|
||
group = DefineGroup('smtp_client', src, depend = ['PKG_USING_SMTP_CLIENT'], CPPPATH = path) | ||
|
||
Return('group') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/************************************************* | ||
Copyright (c) 2019 | ||
All rights reserved. | ||
File name: smtp_client_example.c | ||
Description: smtp发送邮件示例邮件 | ||
History: | ||
1. Version: | ||
Date: 2019-10-14 | ||
Author: wangjunjie | ||
Modify: | ||
*************************************************/ | ||
#include "smtp_client.h" | ||
#include "rtthread.h" | ||
|
||
//若使用TLS加密则需要更大的堆栈空间 | ||
#ifdef SMTP_CLIENT_USING_TLS | ||
#define SMTP_CLIENT_THREAD_STACK_SIZE 4096 | ||
#else | ||
#define SMTP_CLIENT_THREAD_STACK_SIZE 2048 | ||
#endif | ||
|
||
/* | ||
*邮件信息相关宏定义 | ||
*/ | ||
//smtp 服务器域名 | ||
#define SMTP_SERVER_ADDR "smtp.qq.com" | ||
//smtp 服务器端口号 | ||
#define SMTP_SERVER_PORT "25" | ||
//smtp 登录用户名 | ||
#define SMTP_USERNAME "" | ||
//smtp 登录密码(或凭证) | ||
#define SMTP_PASSWORD "" | ||
//smtp 邮件发送方(必须为登录用户名) | ||
#define SMTP_MAIL_FROM SMTP_USERNAME | ||
//smtp 邮件接收方 | ||
#define SMTP_RCPT_TO "" | ||
//邮件主题 | ||
#define SMTP_SUBJECT "SMTP TEST" | ||
|
||
|
||
//邮件内容 | ||
char *content = "THIS IS SMTP TEST\r\n" | ||
"HELLO SMTP\r\n" | ||
"--------------------------------------\r\n" | ||
"based on ---> RT-Thread\r\n" | ||
"based on ---> SMTP_CLIENT\r\n"; | ||
|
||
void smtp_thread(void *param) | ||
{ | ||
//手动延时等待网络初始化成功 | ||
rt_thread_delay(10000); | ||
|
||
//初始化smtp客户端 | ||
smtp_clinet_init(); | ||
//设置服务器地址 | ||
smtp_set_server_addr(SMTP_SERVER_ADDR, ADDRESS_TYPE_DOMAIN, SMTP_SERVER_PORT); | ||
//设置服务器认证信息 | ||
smtp_set_auth(SMTP_USERNAME, SMTP_PASSWORD); | ||
|
||
//发送邮件 | ||
rt_kprintf("\r\n[smtp]: O > start to send mail\r\n"); | ||
if (smtp_send_mail(SMTP_MAIL_FROM, SMTP_RCPT_TO, SMTP_SUBJECT, content) == 0) | ||
{ | ||
//发送成功 | ||
rt_kprintf("\r\n[smtp]: O > send mail success!\r\n"); | ||
} | ||
else | ||
{ | ||
//发送失败 | ||
rt_kprintf("\r\n[smtp]: X > send mail fail!\r\n"); | ||
} | ||
} | ||
|
||
int smtp_thread_entry(void) | ||
{ | ||
rt_thread_t smtp_client_tid; | ||
//创建邮件发送线程(如果选择在主函数中直接调用邮件发送函数,需要注意主函数堆栈大小,必要时调大) | ||
smtp_client_tid = rt_thread_create("smtp", smtp_thread, RT_NULL, SMTP_CLIENT_THREAD_STACK_SIZE, 20, 5); | ||
if (smtp_client_tid != RT_NULL) | ||
{ | ||
rt_thread_startup(smtp_client_tid); | ||
} | ||
return RT_EOK; | ||
} | ||
INIT_APP_EXPORT(smtp_thread_entry); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef __SMTP_H | ||
#define __SMTP_H | ||
#include <stdint.h> | ||
|
||
//域名类型 | ||
#define ADDRESS_TYPE_DOMAIN 0 | ||
//IP地址类型 | ||
#define ADDRESS_TYPE_IP 1 | ||
|
||
//smtp服务初始化 | ||
void smtp_clinet_init(void); | ||
//设置smtp服务器地址和端口 | ||
int smtp_set_server_addr(const char *server_addr, uint8_t addr_type, const char *port); | ||
//设置smtp服务器的用户名密码 | ||
int smtp_set_auth(const char *username, const char *password); | ||
//发送邮件 | ||
int smtp_send_mail(char *from, char *to, char *subject, char *body); | ||
#endif /* __SMTP_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef __SMTP_DATA_H | ||
#define __SMTP_DATA_H | ||
|
||
#include <stdint.h> | ||
|
||
uint32_t smtp_base64_encode(char *target, uint32_t target_len, const char *source, uint32_t source_len); | ||
|
||
#endif /* __SMTP_DATA_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#ifndef __SMTP_PRIVATE_H | ||
#define __SMTP_PRIVATE_H | ||
#include <stdint.h> | ||
#include <rtthread.h> | ||
|
||
#ifdef SMTP_CLIENT_ENABLE_DEBUG_LOG | ||
#define SMTP_LOG rt_kprintf | ||
#else | ||
#define SMTP_LOG(...) | ||
#endif | ||
|
||
#ifdef SMTP_CLIENT_USING_TLS | ||
#include <tls_certificate.h> | ||
#include <tls_client.h> | ||
#endif | ||
|
||
#define SMTP_MAX_ADDR_LEN 100 | ||
#define SMTP_MAX_AUTH_LEN 50 | ||
#define SMTP_SEND_CMD_MAX_LEN 100 | ||
#define SMTP_SEND_DATA_HEAD_MAX_LENGTH 128 | ||
#define SMTP_SEND_DATA_MAX_LEN 512 | ||
#define SMTP_RESPONSE_MAX_LEN 512 | ||
|
||
#ifdef SMTP_CLIENT_USING_TLS | ||
//缓冲区大小 | ||
#define MBEDTLS_READ_BUFFER_LEN 1024 | ||
#endif | ||
|
||
//smtp 会话阶段 | ||
enum smtp_session_state | ||
{ | ||
SMTP_NULL, | ||
SMTP_HELO, | ||
SMTP_START_TLS, | ||
SMTP_FINISH_START_TLS, | ||
SMTP_AUTH_LOGIN, | ||
SMTP_MAIL, | ||
SMTP_RCPT, | ||
SMTP_DATA, | ||
SMTP_BODY, | ||
SMTP_QUIT, | ||
SMTP_CLOSED | ||
}; | ||
|
||
//smtp 会话结构 | ||
typedef struct | ||
{ | ||
//会话状态 | ||
enum smtp_session_state state; | ||
//会话超时时间,如果时间为0,标志超时,则自动关闭连接 | ||
uint16_t timer; | ||
//smtp服务器域名 | ||
const char *server_domain; | ||
//smtp服务器ip | ||
const char *server_ip; | ||
//smtp服务器端口号 | ||
const char *server_port; | ||
//用户名 | ||
char username[SMTP_MAX_AUTH_LEN * 2]; | ||
//密码(有些邮箱服务器需要的是用户凭据) | ||
char password[SMTP_MAX_AUTH_LEN * 2]; | ||
//邮件源地址 | ||
char *address_from; | ||
//邮件目的地址 | ||
char *address_to; | ||
//邮件主题 | ||
char *subject; | ||
//邮件内容 | ||
char *body; | ||
//smtp连接句柄 | ||
int conn_fd; | ||
#ifdef SMTP_CLIENT_USING_TLS | ||
//tls会话 | ||
MbedTLSSession *tls_session; | ||
#endif | ||
} smtp_session_t; | ||
|
||
extern smtp_session_t smtp_session; | ||
|
||
#define SMTP_RESP_220 "220" | ||
#define SMTP_RESP_235 "235" | ||
#define SMTP_RESP_250 "250" | ||
#define SMTP_RESP_334 "334" | ||
#define SMTP_RESP_354 "354" | ||
#define SMTP_RESP_LOGIN_UNAME "VXNlcm5hbWU6" | ||
#define SMTP_RESP_LOGIN_PASS "UGFzc3dvcmQ6" | ||
|
||
#define SMTP_CMD_EHLO "EHLO DM11\r\n" | ||
#define SMTP_CMD_AUTHLOGIN "AUTH LOGIN\r\n" | ||
#define SMTP_CMD_STARTTLS "STARTTLS\r\n" | ||
#define SMTP_CMD_MAIL_HEAD "MAIL FROM: <" | ||
#define SMTP_CMD_MAIL_END ">\r\n" | ||
#define SMTP_CMD_RCPT_HEAD "RCPT TO: <" | ||
#define SMTP_CMD_RCPT_END ">\r\n" | ||
#define SMTP_CMD_DATA "DATA\r\n" | ||
#define SMTP_CMD_HEADER_1 "From: <" | ||
#define SMTP_CMD_HEADER_2 ">\r\nTo: <" | ||
#define SMTP_CMD_HEADER_3 ">\r\nSubject: " | ||
#define SMTP_CMD_HEADER_4 "\r\n\r\n" | ||
#define SMTP_CMD_BODY_FINISHED "\r\n.\r\n" | ||
#define SMTP_CMD_QUIT "QUIT\r\n" | ||
|
||
#ifdef SMTP_CLIENT_USING_TLS | ||
//向SSL/TLS中写入数据 | ||
int smtp_mbedtls_client_write(MbedTLSSession *tls_session, char *buf); | ||
//从 SSL/TLS 中读取数据 | ||
int smtp_mbedtls_client_read(MbedTLSSession *tls_session, char *buf, size_t len); | ||
|
||
//smtp mbedtls 网络连接(用于starttls方式) | ||
int smtp_connect_server_by_starttls(void); | ||
//开启starttls | ||
int smtp_mbedtls_starttls(MbedTLSSession *tls_session); | ||
//smtp 以tls加密方式连接服务器 | ||
int smtp_connect_server_by_tls(void); | ||
//smtp 关闭tls连接,释放资源 | ||
int smtp_mbedtls_close_connection(void); | ||
#endif | ||
|
||
#endif /* __SMTP_PRIVATE_H */ |
Oops, something went wrong.