The use of the ESP8266 in the world of IoT

User avatar
By taoko
#36759 hi, I try to send mail via smtp, but fail. And I do not know how to receive message from smtp.
I use the same code in Telnet, it can work.
If someone can give me a hand, thanks....
Here is the code:
Code: Select all/*
 */

#include <ESP8266WiFi.h>

const char* ssid     = "So-net";
const char* password = "XXXXX";
const char* host = "smtp.gmail.com";


void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 2525;
  if (!client.connect("smtpcorp.com", httpPort)) {
    Serial.println("connection failed");
    return;
  }
  else
    Serial.println("connection ok");
//client.print("HELO smtp.gmail.com\r\n");
  //Serial.println("HELO smtp.gmail.com");
  client.print("EHLO 192.168.0.3\r\n");

  Serial.println("EHLO 192.168.0.3");
  byte respCode;
  byte thisByte;
  int loopCount = 0;
  respCode = client.peek();
  while (client.available())
  {
    Serial.print(client.read(), HEX);
  }

 
  client.print("AUTH LOGIN\r\n");
  Serial.println("AUTH LOGIN");
 
 
  client.print("xxxxxxxxxxxx==\r\n");
  Serial.println("ID");
 
  client.print("xxxxxxx\r\n");  //SECRET password Base64 Encoded
  Serial.println("PassWord");
 
  //client.print("HELO smtp.gmail.com\r\n");
 
  client.print("MAIL FROM:<chentaoko@gmail.com>\r\n");
  Serial.println("MAIL FROM:<chentaoko@gmail.com>");
 
  client.print("RCPT TO:<chentaoko@gmail.com>\r\n");
  Serial.println("RCPT TO:<chentaoko@gmail.com>");
   
 

  client.print("DATA\r\n");
  Serial.println("DATA");
 
  client.print("From: Home Alarm <chentaoko@gmail.com>\r\n");
  Serial.println("From: Home Alarm <chentaoko@gmail.com>");
  //os_sprintf(buff, "MAIL FROM:<%s>\r\n", smtp_conn->user);
 
  client.print("To: <chentaoko@gmail.com>\r\n");
  Serial.println("To: <chentaoko@gmail.com>");
 
  client.print("Subject: ALARM ALERT!\r\n");
  Serial.println("Subject: ALARM ALERT!");
 
  client.print("\r\n");
  client.print("\r\n");
 
  client.print("alarmMessage");
  // 
  client.print(".\r\n");
  Serial.println(".");
  client.print("QUIT\r\n");
  Serial.println("QUIT");

  Serial.println();
  Serial.println("closing connection");
}