littlebot
Published on 2025-04-12 / 1 Visits
0

【源码】基于Arduino的ESP邮件客户端系统

项目简介

本项目是一个Arduino E-Mail客户端库,可在ESP32、ESP8266和SAMD21等设备上实现发送、读取邮件以及获取新邮件通知的功能。该库支持使用Clients接口的其他Arduino设备进行网络通信,能处理多种附件,提供更可靠且灵活的使用方式,无需其他间接的电子邮件代理服务。

项目的主要特性和功能

  1. 多设备支持:支持Espressif的ESP32和ESP8266、Atmel的SAMD21设备以及带有u-blox NINA - W102 WiFi/Bluetooth模块的设备,还支持多种以太网模块和部分使用自定义Clients且闪存大小 > 80k的设备。
  2. 认证机制:支持PLAIN、LOGIN和XOAUTH2认证机制。
  3. 端口支持:支持安全(SSL和TLS或通过STARTTLS升级)和非安全端口。
  4. 邮箱操作:支持邮箱选择用于邮件读取和搜索(IMAP),以及邮箱更改通知(IMAP)。
  5. 内容编解码:支持多种内容编码和解码,如quoted - printable、base64、UTF - 8等。
  6. 嵌入式内容:支持嵌入式内容,如内联图像、附件、并行媒体附件和RFC822消息。
  7. 自定义功能:支持IMAP MIME数据流回调、IMAP自定义字符解码回调、自定义IMAP和SMTP命令。
  8. 调试功能:支持全面调试。
  9. 存储支持:支持使用闪存(ESP32和ESP8266)、SD、SdFat和SD_MMC(ESP32)进行文件存储。
  10. 配置灵活:可自定义配置,通过宏定义可选择编译的类和排除不需要的文件系统库。

安装使用步骤

安装前准备

  1. PlatformIO IDE编译选项:若使用Arduino Nano RP2040 Connect板和PlatformIO IDE,需在platformio.ini中设置lib_ldf_mode = chain+
  2. 移除第三方SD库:在Arduino IDE中,需移除所有安装在libraries文件夹中的第三方SD库,使用Core SD库。
  3. 移除SdFat库(ESP8266):因SdFat与ESP8266核心库冲突,在Arduino IDE中编译ESP8266时需移除该库。
  4. 安装SDK:对于ESP32和ESP8266设备,需安装Arduino的ESP32或ESP8266 Core SDK,建议使用最新版本。

库安装

使用库管理器

  • Arduino IDE:依次点击菜单“Sketch” -> “Include Library” -> “Manage Libraries...”,在搜索框中输入“ESP Mail Client”并选择安装。
  • PlatformIO IDE:点击“PIO Home” -> “Libraries” -> “Registry”,搜索“ESP Mail Client”进行安装。若之前在PlatformIO v2.0.0之前版本的全局存储中安装过该库,升级到v2.0.0及以后版本时,需删除“C:\Users\<UserName>\.platformio\lib”中的库文件,防止编译和运行时出现意外情况。

手动安装

  1. 点击仓库顶部的“Code”下拉菜单,选择“Download ZIP”并保存到本地。
  2. 在Arduino IDE中,依次点击菜单“Sketch” -> “Include Library” -> “Add .ZIP Library...”,选择之前下载的“ESP - Mail - Client - master.zip”。
  3. 将“ESP - Mail - Client - master”文件夹重命名为“ESP_Mail_Client”。
  4. 点击菜单“Files” -> “Examples” -> “ESP Mail Client”,选择一个示例进行使用。

内存选项设置(可选)

ESP8266

  • Arduino IDE:根据设备是否有外部SRAM/PSRAM芯片,选择相应的MMU选项。
  • PlatformIO IDE:在项目的platformio.ini文件中添加相应的build_flags。
  • 芯片连接:若使用外部SRAM/PSRAM芯片,需按指定方式连接。在ESP_Mail_FS.h中定义#define ESP_MAIL_USE_PSRAM以使用外部内存。

ESP32

  • Arduino IDE:在ESP32模块中启用PSRAM。
  • PlatformIO IDE:在platformio.ini文件中添加build_flags = -DBOARD_HAS_PSRAM -mfix - esp32 - psram - cache - issue。在ESP_Mail_FS.h中定义#define ESP_MAIL_USE_PSRAM以使用外部内存。

排除未使用的类以节省内存

ESP_Mail_FS.h中,可通过宏定义启用或禁用IMAP和SMTP类,以及排除不需要的存储文件系统库。

使用示例

可参考examples文件夹中的所有使用示例,以及src/README.md中的函数描述。具体使用示例如下:

发送邮件

```C++ // Include ESP Mail Client library (this library)

include

// Declare the global used SMTPSession object for SMTP transport SMTPSession smtp;

// Declare the global used ESP_Mail_Session for user defined session credentials ESP_Mail_Session session;

void setup() {

Serial.begin(115200);

WiFi.begin("", ""); Serial.print("Connecting to Wi-Fi"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(300); } Serial.println(); Serial.print("Connected with IP: "); Serial.println(WiFi.localIP()); Serial.println();

// Set the session config session.server.host_name = "smtp.office365.com"; // for outlook.com session.server.port = 587; session.login.email = "your Email address"; // set to empty for no SMTP Authentication session.login.password = "your Email password"; // set to empty for no SMTP Authentication session.login.user_domain = "client domain or ip e.g. mydomain.com";

// Set the NTP config time session.time.ntp_server = "pool.ntp.org,time.nist.gov"; session.time.gmt_offset = 3; session.time.day_light_offset = 0;

// Declare the SMTP_Message class variable to handle to message being transport SMTP_Message message;

// Set the message headers message.sender.name = "My Mail"; message.sender.email = "sender or your Email address"; message.subject = "Test sending Email"; message.addRecipient("name1", "email1"); message.addRecipient("name2", "email2");

message.addCc("email3"); message.addBcc("email4");

// Set the message content message.text.content = "This is simple plain text message";

//Base64 data of image const char *greenImg = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAoUlEQVR42u" "3RAQ0AMAgAoJviyWxtAtNYwzmoQGT/eqwRQoQgRAhChCBECEKECBGCECEI" "EYIQIQgRghCECEGIEIQIQYgQhCBECEKEIEQIQoQgBCFCECIEIUIQIgQhCB" "GCECEIEYIQIQhBiBCECEGIEIQIQQhChCBECEKEIEQIQhAiBCFCECIEIUIQ" "ghAhCBGCECEIEYIQIUKEIEQIQoQg5LoBBaDPbQYiMoMAAAAASUVORK5CYII=";

// Declare the attachment data SMTP_Attachment att;

// Set the attatchment info att.descr.filename = "green.png"; att.descr.mime = "image/png"; att.blob.data = (uint8_t *)greenImg; att.blob.size = strlen(greenImg); // Set the transfer encoding to base64 att.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64; // We set the content encoding to match the above greenImage data att.descr.content_encoding = Content_Transfer_Encoding::enc_base64;

// Add attachment to the message message.addAttachment(att);

// Connect to the server smtp.connect(&session / session credentials /);

// Start sending Email and close the session if (!MailClient.sendMail(&smtp, &message)) Serial.println("Error sending Email, " + smtp.errorReason());

} ```

读取邮件

```C++ // Include ESP Mail Client library (this library)

include

// Declare the global used IMAPSession object for IMAP transport IMAPSession imap;

// Declare the global used ESP_Mail_Session for user defined session credentials ESP_Mail_Session session;

void setup() {

Serial.begin(115200);

WiFi.begin("", ""); Serial.print("Connecting to Wi-Fi"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(300); } Serial.println(); Serial.print("Connected with IP: "); Serial.println(WiFi.localIP()); Serial.println();

// Set the session config session.server.host_name = "outlook.office365.com"; //for outlook.com session.server.port = 993; session.login.email = "your Email address"; session.login.password = "your Email password";

// Declare the IMAP_Config object used for user defined IMAP operating options // and contains the IMAP operating result IMAP_Config config;

// Set to enable the message content which will be stored in the IMAP_Config data config.enable.html = true; config.enable.text = true;

// Connect to the server imap.connect(&session / session credentials /, &config / operating options and its result /);

// Open or select the mailbox folder to read the message imap.selectFolder("INBOX");

// Define the message UID (number) which required to fetch or read the message // In this case we will get the UID from the max message number (lastest message) // then imap.getUID and imap.selectedFolder().msgCount() should be called after // calling select or open the folder (mailbox). config.fetch.uid = imap.getUID(imap.selectedFolder().msgCount());

// Empty search criteria to disable the messsage search config.search.criteria.clear();

// Read the Email and close the session MailClient.readMail(&imap);

// Get the message(s) list IMAP_MSG_List msgList = imap.data();

// ESP_MAIL_PRINTF used in the examples is for format printing via debug Serial port // that works for all supported Arduino platform SDKs e.g. AVR, SAMD, ESP32 and ESP8266. // In ESP32 and ESP32, you can use Serial.printf directly.

for (size_t i = 0; i < msgList.msgItems.size(); i++) { // Iterate to get each message data through the message item data IMAP_MSG_Item msg = msgList.msgItems[i];

Serial.println("################################");
ESP_MAIL_PRINTF("Messsage Number: %s\n", msg.msgNo);
ESP_MAIL_PRINTF("Messsage UID: %s\n", msg.UID);
ESP_MAIL_PRINTF("Messsage ID: %s\n", msg.ID);
ESP_MAIL_PRINTF("Accept Language: %s\n", msg.acceptLang);
ESP_MAIL_PRINTF("Content Language: %s\n", msg.contentLang);
ESP_MAIL_PRINTF("From: %s\n", msg.from);
ESP_MAIL_PRINTF("From Charset: %s\n", msg.fromCharset);
ESP_MAIL_PRINTF("To: %s\n", msg.to);
ESP_MAIL_PRINTF("To Charset: %s\n", msg.toCharset);
ESP_MAIL_PRINTF("CC: %s\n", msg.cc);
ESP_MAIL_PRINTF("CC Charset: %s\n", msg.ccCharset);
ESP_MAIL_PRINTF("Date: %s\n", msg.date);
ESP_MAIL_PRINTF("Subject: %s\n", msg.subject);
ESP_MAIL_PRINTF("Subject Charset: %s\n", msg.subjectCharset);

// If the message body is available
if (!imap.headerOnly())
{
  ESP_MAIL_PRINTF("Text Message: %s\n", msg.text.content);
  ESP_MAIL_PRINTF("Text Message Charset: %s\n", msg.text.charSet);
  ESP_MAIL_PRINTF("Text Message Transfer Encoding: %s\n", msg.text.transfer_encoding);
  ESP_MAIL_PRINTF("HTML Message: %s\n", msg.html.content);
  ESP_MAIL_PRINTF("HTML Message Charset: %s\n", msg.html.charSet);
  ESP_MAIL_PRINTF("HTML Message Transfer Encoding: %s\n\n", msg.html.transfer_encoding);
}

}

} ```

下载地址

点击下载 【提取码: 4003】【解压密码: www.makuang.net】