[DemoCode下载] WIFI模块发送电子邮件的方法

[复制链接]
1238|4
 楼主| mintspring 发表于 2024-2-25 21:02 | 显示全部楼层 |阅读模式
在你的 Arduino 代码中,你需要设置 ESP8266 连接到你的 Wi-Fi 网络。你需要提供网络名称(SSID)和密码。
  1. #include <ESP8266WiFi.h>

  2. const char* ssid = "YourWiFiNetworkName";
  3. const char* password = "YourWiFiPassword";

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

  7.     // Connect to Wi-Fi network
  8.     Serial.println();
  9.     Serial.println();
  10.     Serial.print("Connecting to ");
  11.     Serial.println(ssid);

  12.     WiFi.begin(ssid, password);

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

  17.     Serial.println("");
  18.     Serial.println("WiFi connected");
  19.     Serial.println("IP address: ");
  20.     Serial.println(WiFi.localIP());
  21. }

  22. void loop() {
  23.     // Your code here
  24. }
使用SMTP协议发送电子邮件:
一旦你的 ESP8266 连接到了 Wi-Fi 网络,你可以使用 SMTP 协议将电子邮件发送到指定的服务器。你需要设置 SMTP 服务器的地址、端口、电子邮件地址和密码,以及收件人的地址。

以下是一个简单的示例,假设你使用 Gmail 的 SMTP 服务器来发送邮件。

  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>

  3. const char* ssid = "YourWiFiNetworkName";
  4. const char* password = "YourWiFiPassword";
  5. const char* smtpServer = "smtp.gmail.com";
  6. const int smtpPort = 465;
  7. const char* email = "your_email@gmail.com";
  8. const char* password = "your_email_password";
  9. const char* recipient = "recipient_email@example.com";

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

  13.     // Connect to Wi-Fi network
  14.     WiFi.begin(ssid, password);

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

  19.     Serial.println("");
  20.     Serial.println("WiFi connected");
  21.     Serial.println("IP address: ");
  22.     Serial.println(WiFi.localIP());
  23. }

  24. void loop() {
  25.     WiFiClientSecure client;

  26.     if (client.connect(smtpServer, smtpPort)) {
  27.         Serial.println("Connected to SMTP server");
  28.         client.println("EHLO " + WiFi.localIP().toString());
  29.         client.println("AUTH LOGIN");
  30.         client.println(base64::encode(email));
  31.         client.println(base64::encode(password));
  32.         client.println("MAIL FROM:<" + String(email) + ">");
  33.         client.println("RCPT TO:<" + String(recipient) + ">");
  34.         client.println("DATA");
  35.         client.println("Subject: Test Email from ESP8266");
  36.         client.println();
  37.         client.println("This is a test email sent from an ESP8266!");
  38.         client.println(".");
  39.         client.println("QUIT");
  40.         Serial.println("Email sent");
  41.     } else {
  42.         Serial.println("Failed to connect to SMTP server");
  43.     }

  44.     delay(60000); // Send email every minute (for example)
  45. }


 楼主| mintspring 发表于 2024-2-25 21:02 | 显示全部楼层
在实际应用中,你可能需要更多的错误处理和安全性措施。另外,有些电子邮件提供商可能需要你启用特定的安全选项,如使用应用密码或启用“不安全应用访问权限”。
jiekou001 发表于 2024-2-26 17:10 | 显示全部楼层
这种都是有公开方案的。
小明的同学 发表于 2024-2-27 09:57 | 显示全部楼层
网上一搜大把的教程。
xinpian101 发表于 2024-2-28 20:44 | 显示全部楼层
WIFI模块是一个很容易做物联网的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

粉丝
快速回复 在线客服 返回列表 返回顶部