請先到以下網址將你要寄信的gmail帳戶修改安全權限,不然將無法透過應用程式的方式來寄信
https://myaccount.google.com/lesssecureapps?pli=1

image

不同的SMTP SERVER可能會有不同的設定方式,這邊以GMAIL為範例

 

下面是OUTLOOK及GMAIL的SMTP的設置,如果需要可以參考看看

image

image

image

 

完成前面的步驟之後,請將下面的程式碼燒錄到ESP32或ESP8266上

功能很簡單,一上電或是重啟之後,ESP會連到你指定的熱點,一旦連到就會執行寄信的動作

請注意程式碼中備註的地方除了功能說明外還須依照你的熱點,密碼及MAIL....等等需要修改才能使用

程式碼:

 

//請先到以下網址將你要寄信的mail帳戶修改安全權限,不然將無法使用
//https://myaccount.google.com/lesssecureapps?pli=1
#include <Arduino.h>
#if defined(ESP32)
  #include <WiFi.h>
#elif defined(ESP8266)
  #include <ESP8266WiFi.h>
#endif
#include <ESP_Mail_Client.h>

#define WIFI_SSID "XXXXX"//輸入熱點
#define WIFI_PASSWORD "XXXXXXX"//輸入熱點密碼

#define SMTP_HOST "smtp.gmail.com"//這邊用GOOGLE
#define SMTP_PORT 465 //google是465如果寄信的帳戶不是google要另外查

/* The sign in credentials */
#define AUTHOR_EMAIL "XXXXXXXXX@gmail.com"//寄件者MAIL
#define AUTHOR_PASSWORD "XXXXXXXX"//寄件者密碼

/* Recipient's email*/
#define RECIPIENT_EMAIL "XXXXXXXXX@gmail.com"//收件者MAIL

/* The SMTP Session object used for Email sending */
SMTPSession smtp;

/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status);

void setup(){
  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to AP");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(200);
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  /** Enable the debug via Serial port
   * none debug or 0
   * basic debug or 1
  */
  smtp.debug(1);

  /* Set the callback function to get the sending results */
  smtp.callback(smtpCallback);

  /* Declare the session config data */
  ESP_Mail_Session session;

  /* Set the session config */
  session.server.host_name = SMTP_HOST;
  session.server.port = SMTP_PORT;
  session.login.email = AUTHOR_EMAIL;
  session.login.password = AUTHOR_PASSWORD;
  session.login.user_domain = "";

  /* Declare the message class */
  SMTP_Message message;

  /* Set the message headers */
  message.sender.name = "ESP32";//寄件者名稱
  message.sender.email = AUTHOR_EMAIL;//寄件者帳戶
  message.subject = "ESP Test Email";//寄信主旨
  message.addRecipient("Felix", RECIPIENT_EMAIL);//收件者名稱跟他的MAIL

  /*Send HTML message*/
  /*
  String htmlMsg = "<div style=\"color:#2f4468;\"><h1>Hello World!</h1><p>- Sent from ESP board</p></div>";
  message.html.content = htmlMsg.c_str();
  message.html.content = htmlMsg.c_str();
  message.text.charSet = "us-ascii";
  message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
  */

  
  //Send raw text message
  String textMsg = "Hello World! - Sent from ESP board 12/10";//信件內容
  message.text.content = textMsg.c_str();
  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;

  /* Set the custom message header */
  //message.addHeader("Message-ID: <abcde.fghij@gmail.com>");

  /* Connect to server with the session config */
  if (!smtp.connect(&session))
    return;

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

void loop(){

}

/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status){
  /* Print the current status */
  Serial.println(status.info());

  /* Print the sending result */
  if (status.success()){
    Serial.println("----------------");
    ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
    ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
    Serial.println("----------------\n");
    struct tm dt;

    for (size_t i = 0; i < smtp.sendingResult.size(); i++){
      /* Get the result item */
      SMTP_Result result = smtp.sendingResult.getItem(i);
      time_t ts = (time_t)result.timestamp;
      localtime_r(&ts, &dt);

      ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
      ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
      ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
      ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients);
      ESP_MAIL_PRINTF("Subject: %s\n", result.subject);
    }
    Serial.println("----------------\n");
  }
}

 

成功之後你會看到如下

如果成功監控視窗會反饋以下畫面

image

打開MAIL可以看見信件

image

 

 

arrow
arrow
    創作者介紹
    創作者 凶王 的頭像
    凶王

    凶王的部落

    凶王 發表在 痞客邦 留言(0) 人氣()