Serial.print("Connecting...");
50
-
if (client.connect(_smtp_server, _port) <= 0) {
-
Serial.println("connection failed.");
-
} else {
-
Serial.println("connected.");
55
read_response(client);
-
if (!_use_auth) {
-
Serial.println("Using no authentication.");
-
send_line(client, "helo");
-
}
60
else {
-
Serial.println("Using authentication.");
-
send_line(client, "ehlo");
-
send_line(client, "auth login");
-
send_line(client, _username);
65
send_line(client, _password);
-
}
-
send_line(
-
client,
-
"mail from: <" + email.getFrom() + ">"
70
);
-
send_line(
-
client,
-
"rcpt to: <" + email.getTo() + ">"
-
);
75
send_line(client, "data");
-
send_line(client, "from: " + email.getFrom());
-
send_line(client, "to: " + email.getTo());
-
send_line(client, "subject: " + email.getSubject());
-
send_line(client, "");
80
send_line(client, email.getBody());
-
send_line(client, ".");
-
send_line(client, "quit");
-
client.println("Disconnecting.");
-
client.stop();
85
}
-
}
-
};
-
-
#endif
90
Admittedly, this is a lot of code, but we’ll walk through it step by step. First,
the
SmtpService
class encapsulates the SMTP server’s IP address and its port.
These attributes are required in any case. In addition, we store a username
and a password in case someone’s going to use an authenticated connection.
To communicate with an SMTP server, we have to read its responses, and we
do that using the private
read_response
method starting on line 13. It waits for
report erratum • discuss
Emailing Directly from an Arduino • 191
www.it-ebooks.info