Arduino library 路 Network

ESP Mail Client: send an email from an ESP32

ESP Mail Client, also by Mobizt, sends email from an ESP32 or ESP8266 over SMTP and can read a mailbox over IMAP. The common use is an alert: the door opened, the freezer warmed up, the plant needs water, and a message lands in your inbox with no app or cloud dashboard in between.

Built in on Mokxi: #include <ESP_Mail_Client.h> and it builds, with nothing to install. Free, and no account needed.

What ESP Mail Client does

Sending takes four objects. An SMTPSession is the connection. A Session_Config holds the server, port, login email and password. An SMTP_Message holds the sender, subject, recipients and body. MailClient.sendMail(&smtp, &message) puts them together, and a status callback plus smtp.sendingResult tell you how it went. Gmail, Outlook and most providers want TLS on port 465 or STARTTLS on 587.

In Mokxi the library compiles and the Send_Text example runs on an ESP32-C3, but no mail goes out: SMTP needs a TCP connection with TLS to the mail server, which neither the simulated network nor Real internet mode carries. smtp.connect() and sendMail() return false with errorCode() -1 and an errorReason() that explains, and the serial monitor says so. IMAP is not included. For a notification you can see in the simulator, POST to http://api.mokxi/ with HTTPClient.

Library Manager nameESP Mail Client
AuthorMobizt
Include#include <ESP_Mail_Client.h>
BoardsESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU
In the simulatorBuilds and runs, and says plainly that the hardware behind it is not simulated.

Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU.

In Mokxi it is built in: #include <ESP_Mail_Client.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ESP Mail Client" by Mobizt from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add ESP Mail Client to lib_deps in PlatformIO. Mokxi follows the calls of release 3.4.24, so a sketch written here builds there too.

What to know in the simulator: SMTP needs a TCP connection with TLS to the mail server, which neither the simulated network nor Real internet mode carries, so smtp.connect() and sendMail() return false with errorCode() -1 and an errorReason() that says why, and one Serial line says it too. IMAP is not here. HTTPClient with http://api.mokxi/ shows a notification request in the Cloud panel.

ESP32-C3 路 ESP32-C6 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32 DevKit 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 ESP32-S3-DevKitC-1 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 ESP8266 NodeMCU

The calls you will use

In the order a sketch meets them.

SMTPSession smtp

The SMTP connection object, declared globally.

Session_Config config

The server and login: set config.server.host_name, config.server.port, config.login.email and config.login.password.

config.server.host_name = "smtp.gmail.com";
config.server.port = esp_mail_smtp_port_465;
config.login.email = AUTHOR_EMAIL;
config.login.password = APP_PASSWORD;

SMTP_Message message

The email itself: message.sender.email, message.subject and message.text.content, plus recipients.

message.addRecipient(F("Me"), "me@example.com")

Adds a recipient by display name and address. Call it once per recipient.

smtp.callback(smtpCallback)

Registers a function given an SMTP_Status as sending progresses, with success() and info() to print.

smtp.connect(&config)

Connects and logs in. Returns false on failure; smtp.errorReason() says why.

MailClient.sendMail(&smtp, &message)

Sends the message and closes the session. Returns true when the server accepted it.

smtp.errorReason()

The last error as a String, with errorCode() and statusCode() for the numbers.

Example: Sending an email, and why it cannot go out here

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino 路 ESP32-C3-DevKitM-1
// After the library's Send_Text example.
#include <WiFi.h>
#include <ESP_Mail_Client.h>

#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT esp_mail_smtp_port_465
#define AUTHOR_EMAIL "board@example.com"
#define AUTHOR_PASSWORD "app-password"
#define RECIPIENT_EMAIL "me@example.com"

SMTPSession smtp;

void smtpCallback(SMTP_Status status) {
  Serial.println(status.info());
  if (status.success()) {
    ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
  } else {
    ESP_MAIL_PRINTF("Message sent failed: %d\n", status.failedCount());
  }
}

void setup() {
  Serial.begin(115200);
  WiFi.begin("Mokxi-Lab", "breadboard");
  while (WiFi.status() != WL_CONNECTED) delay(250);
  MailClient.networkReconnect(true);
  smtp.debug(1);
  smtp.callback(smtpCallback);

  Session_Config config;
  config.server.host_name = SMTP_HOST;
  config.server.port = SMTP_PORT;
  config.login.email = AUTHOR_EMAIL;
  config.login.password = AUTHOR_PASSWORD;
  config.login.user_domain = "";

  SMTP_Message message;
  message.sender.name = F("ESP");
  message.sender.email = AUTHOR_EMAIL;
  message.subject = F("ESP Test Email");
  message.addRecipient(F("Me"), RECIPIENT_EMAIL);
  message.text.content = "Hello World! - Sent from ESP board";
  message.text.charSet = "us-ascii";
  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
  message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;
  message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;

  if (!smtp.connect(&config)) {
    ESP_MAIL_PRINTF("Connection error, Status Code: %d, Error Code: %d, Reason: %s\n", smtp.statusCode(),
                    smtp.errorCode(), smtp.errorReason().c_str());
  }
  if (!MailClient.sendMail(&smtp, &message)) {
    ESP_MAIL_PRINTF("Error, Status Code: %d, Error Code: %d, Reason: %s\n", smtp.statusCode(), smtp.errorCode(),
                    smtp.errorReason().c_str());
  }
  SMTP_Result result = smtp.sendingResult.getItem(0);
  ESP_MAIL_PRINTF("Status: %s, Recipient: %s, Subject: %s\n", result.completed ? "success" : "failed",
                  result.recipients.c_str(), result.subject.c_str());
}

void loop() {}

Open it in the editor, wired and ready to run

Common errors and how to fix them

authentication failed, or 535 Username and Password not accepted (Gmail)

Gmail no longer accepts your normal password from devices like this. Turn on 2-Step Verification for the account, create an app password in its security settings, and put that 16-character password in config.login.password.

Connection error, Error Code: -1

The board could not open a TLS connection to the mail server: check the host name, use port 465 for SSL or 587 for STARTTLS, and make sure WiFi is up and the clock is set. In Mokxi this is the expected result, since SMTP is not carried.

sendMail() says success but nothing arrives

The server accepted the message and then filtered it. Look in the spam folder, make sure the sender address is the account you logged in with, and avoid sending dozens of identical alerts in a few minutes.

Questions and answers

Can I send email from Mokxi at all?

Not over SMTP. The sketch compiles and runs so you can check it, then tells you it cannot connect. For a cloud notification you can watch, use HTTPClient to POST to http://api.mokxi/ and look in the Cloud panel.

Try ESP Mail Client without installing anything

Open the example, change the sketch, and keep your version in a free account.