I am working on sending an email alert from my Arduino. I cannot seem to find any successful programs using login authentication. The email server I am using does not require SSL or TLS protocols but does require login authentication. I can login in no problem with the code I have but it never sends an email. I think it is due to the CC but I am not sure. Below is what I have. Any assistance would be greatly appreciated. I took out the emails and passwords for obvious reasons.
.ino file
// Jayconsystems.com
// Based on the excellent guide at:
// www.scribd.com/doc/88533821/Arduino-Et-Internet-A-Quick-Start-Guide
#include <SPI.h>
#include <EthernetV2_0.h>
#include <Smtp_Service.h>
const unsigned int SMTP_PORT = 26;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = {192,168,1,177};
byte smtp_server[] = { XXX, XXX, XXX, XXX}; //this is changed on the arduino i have running
SmtpService smtp_service(smtp_server, SMTP_PORT);
char incString[250];
String domain = "mail.mydomain.com"; //this is changed on the arduino I have running
String login = "XXXXXX"; //this is changed on the arduino I have running
String password = "XXXXXXX"; //this is changed on the arduino I have running
Email email;
void setup()
{
Ethernet.begin(mac, my_ip);
Serial.begin(115200);
Serial.setTimeout(500000);
delay(1000);
email.setDomain(domain);
email.setLogin(login);
email.setPassword(password);
email.setFrom("XXXXXXXXX"); //this is changed on the arduino I have running
email.setTo("XXXXXXXXX"); //this is changed on the arduino I have running
email.setCc("");
email.setSubject("First Try");
email.setBody("It really is =)");
Serial.print("Enter S to Begin");
}
void loop()
{
if (Serial.available() > 0) {
byte inByte = Serial.read();
if (inByte == 'S')
{
smtp_service.send_email(email);
}
delay(1000);
}
}
email.h
#ifndef __EMAIL__H_
#define __EMAIL__H_
class Email
{
String _domain, _login, _password, _from, _to, _cc, _subject, _body;
public:
Email(
const String& domain = "",
const String& login = "",
const String& password = "",
const String& from = "",
const String& to = "",
const String& cc = "",
const String& subject = "",
const String& body = ""
) : _domain(domain), _login(login), _password(password), _from(from), _to(to), _cc(cc), _subject(subject), _body(body) {}
const String& getDomain() const { return _domain; }
const String& getLogin() const { return _login; }
const String& getPassword() const { return _password; }
const String& getFrom() const { return _from; }
const String& getTo() const { return _to; }
const String& getCc() const { return _cc; }
const String& getSubject() const { return _subject; }
const String& getBody() const { return _body; }
void setDomain( const String domain) { _domain = domain; }
void setLogin( const String login) { _login = login; }
void setPassword( const String password) { _password= password; }
void setFrom( const String from) { _from = from; }
void setTo( const String to) { _to = to; }
void setCc( const String cc) { _cc = cc; }
void setSubject( const String subject) { _subject = subject; }
void setBody( const String body) { _body = body; }
};
#endif
Smtp_Service.h
#ifndef __SMTP_SERVICE__H_
#define __SMTP_SERVICE__H_
#include "email.h"
class SmtpService
{
byte* _smtp_server;
unsigned int _port;
void read_response(Client& client)
{
delay(5000);
while(client.available())
{
const char c = client.read();
Serial.print(c);
}
}
void send_line(Client& client, String line)
{
const unsigned int MAX_LINE = 256;
char buffer[MAX_LINE];
line.toCharArray(buffer, MAX_LINE);
Serial.println(buffer);
client.println(buffer);
read_response(client);
}
public:
SmtpService(
byte* smtp_server,
const unsigned int port) : _smtp_server(smtp_server),
_port(port) {}
void send_email (const Email& email)
{
EthernetClient client;
Serial.print("Connecting...");
if(!client.connect(_smtp_server, _port))
{
Serial.println("connection failed.");
}
else
{
Serial.println("Connected.");
read_response(client);
send_line(client, String("ehlo ") + email.getDomain());
send_line(client, String("AUTH LOGIN"));
send_line(client, email.getLogin());
send_line(client, email.getPassword());
send_line(client, String("mail from: <") + email.getFrom() + String(">"));
send_line(client, String("rcpt to: <") + email.getTo() + String(">"));
send_line(client, String("rcpt to: <") + email.getCc() + String(">"));
send_line(client, String("data"));
send_line(client, String("from: ") + email.getFrom());
send_line(client, String("to: ") + email.getTo());
send_line(client, String("subject: ") + email.getSubject());
send_line(client, String(""));
send_line(client, email.getBody());
send_line(client, String("."));
send_line(client, String("quit"));
client.println("Disconnecting.");
client.stop();
}
}
};
#endif
Serial Monitor Output
Enter S to BeginConnecting...Connected.
220-host353.hostmonster.com ESMTP Exim 4.84 #2 Wed, 29 Apr 2015 14:09:27 -0600
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.
ehlo mail.mydomain.com
250-host353.hostmonster.com Hello mail.mydomain.com [XXX.XXX.XXX.XXX]
250-SIZE 52428800
250-8BITMIME
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
AUTH LOGIN
334 VXNlcm5hbWU6
XXXXXXXXXX
334 UGFzc3dvcmQ6
XXXXXXXXXX
235 Authentication succeeded
mail from: <XXXXXXXXXX.com>
250 OK
rcpt to: <XXXXXXXXXXl.com>
250 Accepted
rcpt to: <>
501 <>: missing or malformed local part
data
354 Enter message, ending with "." on a line by itself
from: XXXXXXXXXX.com
to: XXXXXXXXXX.com
subject: First Try
It really is =)
.
quit
When I attempt to put something into the cc line, it will no longer connect unless its a third email.
Even with the third email it does not work.
Any assistance would be greatly appreciated.