Hello there.
I am trying to send an email using Arduino, Ethernet Shield and SMTP service.
in the serial monitor it said that the email was sent, but when I check both the sender and receiver emails I do not find any.
Do you have any idea about the reason?
Thank you for your help.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
// MAC address for the Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Network configuration
IPAddress ip(192, 168, 0, 36); // Replace with your static IP address
IPAddress dns(192, 168, 0, 1); // Replace with your DNS server IP address
IPAddress gateway(192, 168, 0, 1); // Replace with your gateway/router IP address
IPAddress subnet(255, 255, 255, 0); // Replace with your subnet mask
// SMTP server settings
const char* smtpServer = "mail.smtp2go.com";
const int smtpPort = 8465;
const char* smtpUser = "****"; // Replace with your SMTP2GO username
const char* smtpPass = "*****"; // Replace with your SMTP2GO password
// Email settings
const char* fromEmail = "******"; // Replace with your email address
const char* toEmail = "*******"; // Replace with the recipient's email address
const char* subject = "Pin 8 Triggered!";
const char* message = "The input on pin 8 was triggered.";
EthernetClient client;
void setup() {
Serial.begin(9600);
// Initialize pin 8 as an input
pinMode(8, INPUT);
// Initialize the Ethernet shield
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Try with static IP
Ethernet.begin(mac, ip, dns, gateway, subnet);
} else {
Serial.println("Ethernet configured via DHCP");
}
// Allow the hardware to start up
delay(1000);
Serial.println("Network configuration:");
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server: ");
Serial.println(Ethernet.dnsServerIP());
}
void loop() {
// Check if pin 8 is HIGH
if (digitalRead(8) == HIGH) {
sendEmail();
delay(10000); // Delay to prevent multiple emails being sent in quick succession
}
}
void sendEmail() {
Serial.println("Connecting to SMTP server...");
if (client.connect(smtpServer, smtpPort)) {
Serial.println("Connected to SMTP server");
// Sending email headers
client.println("EHLO smtp2go.com");
readResponse();
client.println("AUTH LOGIN");
readResponse();
client.println(encodeBase64(smtpUser));
readResponse();
client.println(encodeBase64(smtpPass));
readResponse();
// Sending email content
client.println("MAIL FROM:<" + String(fromEmail) + ">");
readResponse();
client.println("RCPT TO:<" + String(toEmail) + ">");
readResponse();
client.println("DATA");
readResponse();
client.println("Subject: " + String(subject));
client.println("To: " + String(toEmail));
client.println("From: " + String(fromEmail));
client.println();
client.println(String(message));
client.println(".");
readResponse();
client.println("QUIT");
readResponse();
Serial.println("Email sent");
} else {
Serial.println("Failed to connect to SMTP server");
}
client.stop();
}
void readResponse() {
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}
}
String encodeBase64(const char* input) {
const char* base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String encoded;
int i = 0, j = 0;
unsigned char char_array_3[3], char_array_4[4];
while (*input) {
char_array_3[i++] = *(input++);
if (i == 3) {
char_array_3_to_4(char_array_3, char_array_4);
for (i = 0; (i < 4); i++) {
encoded += base64_chars[char_array_4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
char_array_3[j] = '\0';
}
char_array_3_to_4(char_array_3, char_array_4);
for (j = 0; (j < i + 1); j++) {
encoded += base64_chars[char_array_4[j]];
}
while ((i++ < 3)) {
encoded += '=';
}
}
return encoded;
}
void char_array_3_to_4(unsigned char* char_array_3, unsigned char* char_array_4) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
}