Email with Login Authentication

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.

Normally when the email server sends a failure response, like a 501 message, the client should quit the connection. My email code does. Any response that starts with a 2 or 3 is ok. Any response that starts with a 4 or 5 is not.

edit: I think it is complaining about this:

   email.setCc("");

SuerferTim,

Do you happen to have code similar to your playground that includes a username and password? That is my big issue. I am using Comcast and they require authentication even on port 25.

Actually, yours looks like it would work if you didn’t try sending an empty CC. Have you tried without this line?

      send_line(client, String("rcpt to: <") + email.getCc() + String(">"));

I removed the line from the .h file, I do not receive any errors, but no email was still sent. Below is what is in the Serial Monitor

Enter S to BeginConnecting...Connected.
220 resomta-po-10v.sys.comcast.net comcast ESMTP server ready
ehlo smpt.comcast.net
250-resomta-po-10v.sys.comcast.net hello [73.220.176.13], pleased to meet you
250-HELP
250-AUTH LOGIN PLAIN
250-SIZE 36700160
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-STARTTLS
250 OK
AUTH LOGIN
334 VXNlcm5hbWU6
XXXXXXXXXXX
334 UGFzc3dvcmQ6
XXXXXXXXXXX
235 2.7.0 ... Authentication succeeded
mail from: <XXXXXXXXX>
250 2.1.0 <XXXXXXXXX> sender ok
rcpt to: <XXXXXXXXXX>
250 2.1.5 <XXXXXXXXX> recipient ok
data
354 enter mail, end with "." on a line by itself
from: XXXXXXXXX
to: XXXXXXXXX
subject: First Try

It really is =)
.
quit

I apologize, the last serial data file was with a different email server but the same result.
Any other ideas? I am really at a loss. Based on what I have read it should be working.
The Arduino is plugged directly into the router by the way.

There should be a response from the server after you send the period on a line by itself. I don't see it.

It really is =)
.
// right here should be a response from the server saying "email sent"
quit

Thanks Surfer, I am at a loss.
I am supposed to use the outgoing smtp server and port correct?
Maybe I will try a different PC.
I will post a solution if I find out what is wrong.

Ditchpanther:
Thanks Surfer, I am at a loss.
I am supposed to use the outgoing smtp server and port correct?
Maybe I will try a different PC.
I will post a solution if I find out what is wrong.

Hi

Download the source code from my Arduino application at http://www.2wg.co.nz. On the SD card web page goes into the PUBLIC folder to access the source code files. In the utility.h and .cpp files look for my EmailInitialiseXM(), EmailLineXM() and EmailDisconnectXM() (transmit) and the CheckEmailResponse() procedures.

If you set up a #define EMAILDebug conditional define you will get some debugging information output to the Serial Monitor.

This code works very well for me - I use it to send email alerts to my iPhone (as push emails) for all sorts of events.

I hope this helps.

Cheers

Catweazle NZ.