/*
* gmailアカウントを使ってメールする
*/
#include <ESP8266WiFi.h>
const char* SSID = "SSID";
const char* PASS = "PASSWORD";
#define SMTP_SERVER "smtp.gmail.com"
#define SMTP_PORT 465
#define BASE64_USER "BASE64変換したgmailアカウント" //
Username of gmail
#define BASE64_PASS
"BASE64変換したパスワード" //
Password of gmail
#define MAIL_FROM "aaa@gmail.com"
// gmail account
#define MAIL_TO
"aaa@biglobe.ne.jp"
// Mail To
//WiFiClient client;
WiFiClientSecure client;
void setup()
{
Serial.begin(9600);
delay(10);
Serial.println("");
Serial.println("");
Serial.print("Connecting To ");
Serial.println(SSID);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
byte ret = sendEmail();
}
void loop()
{
}
byte sendEmail()
{
// byte thisByte = 0;
// byte respCode;
char buff[64];
// if (client.connect(server, port) == 1) {
if (client.connect(SMTP_SERVER, SMTP_PORT)) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if (!eRcv()) return 0;
Serial.println(F("Sending EHLO"));
client.println("EHLO www.example.com");
if (!eRcv()) return 0;
Serial.println(F("Sending auth login"));
client.println("auth login");
if (!eRcv()) return 0;
Serial.println(F("Sending User"));
// Change to your base64, ASCII encoded user
client.println(BASE64_USER);
//<---------Username
if (!eRcv()) return 0;
Serial.println(F("Sending Password"));
// change to your base64, ASCII encoded password
client.println(BASE64_PASS);
//<---------Password
if (!eRcv()) return 0;
Serial.println(F("Sending From"));
sprintf(buff,"MAIL From: <%s>",MAIL_FROM);
client.println(buff);
if (!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To"));
sprintf(buff,"RCPT To: <%s>",MAIL_TO);
client.println(buff);
if (!eRcv()) return 0;
Serial.println(F("Sending DATA"));
client.println(F("DATA"));
if (!eRcv()) return 0;
Serial.println(F("Sending email"));
sprintf(buff,"To: You <%s>",MAIL_TO);
client.println(buff);
sprintf(buff,"From: Me <%s>",MAIL_FROM);
client.println(buff);
client.println("Subject: Mail from ESP8266\r\n");
client.println("This is from my ESP8266\n");
// client.println(F("This is line 2 from my
ESP8266"));
// client.println(F("This is line 3 from my
ESP8266"));
// client.println(F("This is line 3 from my
ESP8266"));
client.println("ArduinoのスケッチEDITORはUTF-8なので、日本語も送ることができます");
client.println(F("."));
if (!eRcv()) return 0;
Serial.println(F("Sending QUIT"));
client.println(F("QUIT"));
if (!eRcv()) return 0;
client.stop();
Serial.println(F("disconnected"));
return 1;
}
byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;
while (!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds,
timeout
if (loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
respCode = client.peek();
while (client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
if (respCode >= '4')
{
// efail();
return 0;
}
return 1;
}
|