Email Send Error

I’m sending an email using Arduino Mega2560 with ethernet shield but I’m receiving and error after “sending”. The error is :

Email Sent
220 BLU0-SMTP174.phx.gbl Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at Fri, 4 May 2012 19:05:14 -0700
501 5.5.4 Invalid Address
530 5.7.0 Must issue a STARTTLS command first

The code is

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };//Ethernet Shiel MAC
IPAddress ip( 1**, 1**, 1**, 1** );//IP Address of the connecting network    
IPAddress gateway( 1**, 1**, 1**, 1** );
IPAddress subnet ( 1**, 1**, 1**, 1** );
IPAddress server (65,55,162,200);//Hotmail IP Address Server
int x= 1000000;
int y=5000;

EthernetClient client;

void setup()
{
  delay(y);
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  delay (y);
  Serial.println ("connecting");
  delay(y);
  if (client.connect (server,25)) {
    Serial.println ("connected");
    client.write ("HELO hotmail.com");
    delay (x);
    client.write ("AUTH");
    delay(x);
    client.write (""); //user aut.
    delay(x);
    client.write ("");// password aut.
    delay(x);
     client.write ("Mail From:<@hotmail.com>");
     Serial.println ("Mail from");
    delay(x);
    client.write ("RCPT To:<@gmail.com>");
     Serial.println ("Mail To");
    delay(x);
    client.write ("DATA");
     Serial.println ("Data");
    delay(x);
    client.write ("From:<@hotmail.com>");
    Serial.println ("From");
    delay(x);
    client.write ("To:<@gmail.com");
    Serial.println ("To");
    delay(x);
    client.write ("Subject: ");
     Serial.println ("Subject");
    delay(x);
    client.write ("Trial");
    delay(x);
    client.println ();
    client.write ("Espero que alla funcionado");
    Serial.println ("Body");
    delay(x);
    client.println ("Quit");
    delay(x);
    Serial.println("Email Sent");
   
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for(;;)
     ;
 }
}

501 5.5.4 Invalid Address 530 5.7.0 Must issue a STARTTLS command first

To send email to a destination address other than Hotmail, you must authenticate with your user and password, but it requires Transport Layer Security (TLS) to do that. The ethernet shield library has no TLS capability. http://en.wikipedia.org/wiki/Email_encryption

I have to authenticate the user name and password from the sending or receiving email? I took other examples program that says it works,then there is no way I could send an email to either hotmail to hotmail, gmail to gmail or combined? There is a way to make ethernet shield compatible with TLS?

Change network settings and email addresses. Look for “// change this” comments.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
IPAddress ip( 192, 168, 2, 2 );    
IPAddress gateway( 192, 168, 2, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// gmail.com email server
IPAddress server( 74, 125, 65, 27 );

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  delay(2000);
  Serial.println("Ready. Press 'e' to send");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println("Email sent");
      else Serial.println("Email failed");
  }
}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  
  if (client.connect(server,25)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

// change this ip to your public ip
  client.write("helo 1.2.3.4\r\n");

  if(!eRcv()) return 0;

// change this
  client.write("MAIL From: <myemail@gmail.com>\r\n");
  
  if(!eRcv()) return 0;

// change this  
  client.write("RCPT To: <youremail@gmail.com>\r\n");

  if(!eRcv()) return 0;

  client.write("DATA\r\n");

  if(!eRcv()) return 0;

//change this
  client.write("To: You <youremail@gmail.com>\r\n");

// change this
  client.write("From: Me <myemail@gmail.com>\r\n");
  
  client.write("Subject: Arduino email test\r\n");
  
  client.write("This is from my Arduino!\r\n");
  
  client.write(".\r\n");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("disconnected");
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  
  while(!client.available()) delay(1);

  respCode = client.peek();

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }
  
  return 1;
}

void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println("disconnected");
}

edit: My apology in advance for any typos. I had to edit out my settings. I just returned from surfing and I need to get some surf wax and something to eat now. :slight_smile:

In ther part that says

// change this ip to your public ip client.write("helo 1.2.3.4\r\n");

What I change there?

Replace 1.2.3.4 with your public ip. Itis normally the one assigned to the WAN interface on your router.

This is the respond I get

Ready. Press ‘e’ to send
connected
220 mx.google.com ESMTP i7sm19636491ani.17
250 mx.google.com at your service
530 5.7.0 Must issue a STARTTLS command first. i7sm19636491ani.17
221 2.0.0 closing connection i7sm19636491ani.17
disconnected
Email failed

Using the code you give as

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  
IPAddress ip( 192, 168, 1, 107 );    
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// gmail.com email server
IPAddress server( 74, 125, 45, 109 );
int x=5000;
int y=20000;

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  delay(x);
  Serial.println("Ready. Press 'e' to send");
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println("Email sent");
      else Serial.println("Email failed");
  }
}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  
  if (client.connect(server,25)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

// change this ip to your public ip
  client.write("helo 70.45.71.79\r\n");

  if(!eRcv()) return 0;

// change this
  client.write("MAIL From: <@gmail.com>\r\n");
  
  if(!eRcv()) return 0;

// change this  
  client.write("RCPT To: <@gmail.com>\r\n");

  if(!eRcv()) return 0;

  client.write("DATA\r\n");

  if(!eRcv()) return 0;

//change this
  client.write("To: You <@gmail.com>\r\n");

// change this
  client.write("From: Me <@gmail.com>\r\n");
  
  client.write("Subject: Arduino email test\r\n");
  
  client.write("This is from my Arduino!\r\n");
  
  client.write(".\r\n");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("disconnected");
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  
  while(!client.available()) delay(x);

  respCode = client.peek();

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }
  
  return 1;
}

void efail()
{
  byte thisByte = 0;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println("disconnected");
}

530 5.7.0 Must issue a STARTTLS command first. i7sm19636491ani.17

That is asking you to start TLS, but you can't. There must be something about your email addresses that is not correct. I am on a different computer now. Give me a second and I will try the code and post my output.

I used the same code and server/port with my gmail account, and here is the output

Ready connected 220 mx.google.com ESMTP e5si4439592anh.161 250 mx.google.com at your service 250 2.1.0 OK e5si4439592anh.161 250 2.1.5 OK e5si4439592anh.161 354 Go ahead e5si4439592anh.161 250 2.0.0 OK 1336249314 e5si4439592anh.161 221 2.0.0 closing connection e5si4439592anh.161 disconnected Email sent

...and DING! My Blackberry says there is email in my Gmail account.

edit: By the place it asked for that TLS on your attempt, it must not like your FROM email address or your ip address.

Does your ISP have an email server you can send through? It may take email from the ips in its "localnet".

Does your ISP have an email server you can send through? It may take email from the ips in its "localnet".

My ISP don't give me any email server,I'm form Puerto Rico. I don't know if here the ISP works diferent.

edit: By the place it asked for that TLS on your attempt, it must not like your FROM email address or your ip address.

What do you mean there? My email is working account and the IP I'm using is one random by the router original ip address?

Where the server sent that 530 response is after the Arduino code sent the FROM email address. It had not even told the server what email address you wanted to send to. It didn't get that far.

That is why I use this code. That is normally the response if you use port 465 or 587, not port 25. You left the port set to 25 correct? Let me break it down.

220 mx.google.com ESMTP e5si4439592anh.161 // You send helo 250 mx.google.com at your service // You send "MAIL From:". Here is where the 503 error was returned on yours. 250 2.1.0 OK e5si4439592anh.161 // You send "RCPT To:" 250 2.1.5 OK e5si4439592anh.161 // You send "DATA" 354 Go ahead e5si4439592anh.161 // You send the email with those to, from, and subject sends // with a period on its own line as the terminator 250 2.0.0 OK 1336249314 e5si4439592anh.161 // You send "QUIT" 221 2.0.0 closing connection e5si4439592anh.161 // You client.stop() disconnected Email sent

Yes I use the port 25. So the problem can be in the port that I'm using? Because if the email is a real email form gmail and don't recognizes maybe is the port or the ip server. I'm using in command prompt ping smtp.gmail.com .

You do not have a connection problem. All looks good until you send the "MAIL From:". That is when you are getting the 530 error about the TLS. You are getting an error message from the server. There is no connection problem. It is a ip/spam blocker/protocol challenge.

Maybe you should use nslookup and see if there is another gmail email server ip for your area. It should be the MX entry.

Ok thank you,but sorry for this....how do I look up for that nslookup?

My bad. It is a bit complex. From a command prompt

nslookup
set query=any
gmail.com

You should see a list of "MX preference = X mail exchanger = servername". Find the lowest number. Then below that is a list of those servernames with the ip of each.

Thanks a lot mannnn!!!! You are my hero jajajajaja

It only send gmail to gmail?

It will only send TO gmail email addresses. The only real limitation to the FROM address is the domain (the part after the @) must be a valid domain name.

If you want to send to users in other domains, use the same procedures (including nslookup) to send to those domains.

FYI: Despite how it may appear, I am not a spammer. I know email servers.

edit: This is closer to an email server than an email client. It works like Sendmail or PostFix, not Outlook or Thunderbird.

Wonderful information.

I am also trying to send a simple message from Arduino to my gmail account. Could you please post your finished code?

Thanks, Pura Vida

See reply #3. That code works.

Thanks. It appears I just can't connect to gmail. If I enter "(client.connect("smtp.gmail.com",25)" I get a connection but refused because it's expecting TLS. Output from Arduino: connected 220 mx.google.com ESMTP nt7sm7991698pbb.33 250 mx.google.com at your service 530 5.7.0 Must issue a STARTTLS command first. nt7sm7991698pbb.33 221 2.0.0 closing connection nt7sm7991698pbb.33

Looking for a bypass IP nslookup gives me this: Server: 208.67.222.222 Address: 208.67.222.222#53

Non-authoritative answer: gmail.com mail exchanger = 10 alt1.gmail-smtp-in.l.google.com. gmail.com mail exchanger = 5 gmail-smtp-in.l.google.com. gmail.com mail exchanger = 30 alt3.gmail-smtp-in.l.google.com. gmail.com mail exchanger = 40 alt4.gmail-smtp-in.l.google.com. gmail.com mail exchanger = 20 alt2.gmail-smtp-in.l.google.com. gmail.com nameserver = ns4.google.com. gmail.com nameserver = ns1.google.com. gmail.com nameserver = ns3.google.com. gmail.com nameserver = ns2.google.com. gmail.com origin = ns1.google.com mail addr = dns-admin.google.com serial = 2012061200 refresh = 21600 retry = 3600 expire = 1209600 minimum = 300

Authoritative answers can be found from:

nslookup gmail-smtp-in.l.google.com Server: 208.67.222.222 Address: 208.67.222.222#53

** server can't find nslookup: NXDOMAIN

nslookup alt3.gmail-smtp-in.l.google.com Server: 208.67.222.222 Address: 208.67.222.222#53

And finally the list of IPs that didn't connect from Arduino code: // gmail.com email server // IPAddress server( 74, 125, 65, 27 ); // IPAddress server( 74, 125, 224, 150 ); IPAddress server( 208, 67, 222, 222); // IPAddress server(10, 64, 41, 98); // IPAddress server( 74, 125, 224, 118); EthernetClient client;

So . . . My goal is to have an Arduino watching our basic little concrete place in Costa Rica and email me if there is a problem with the power or alarm. (wonderful surfing nearby) This gmail approach might be too unreliable to let just sit for months. I have a small website with a submit form that would email me if I can get the Arduino to emulate the "post". It has lots of codes and IDs but if I could capture the post string it would be an alternative.

Is gmail likely to change IPs even if I find one that works? Other suggestions would be very appreciated.