Arduino+Wifi Shield sending email error

Hello,can anyone tell me why how to solve these errors (unrecognized command,invalid base 64 data)shows in serial monitor when i use Arduino+Wifi Shield to email ,below attachment is the coding ,thanks! :smiley:

Email2.ino (6.71 KB)

#include <WiFi.h>
#include <SPI.h>
//#include <FLASH.h>
#include <EEPROM.h>

#define FS(x) (__FlashStringHelper*)(x)

char ssid = “iPhone”; // your network SSID (name)
char pass = “miaomiaomiao”; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio’s status

WiFiClient client; // Wifi client instead of Ethernet

IPAddress server(207,58,147,66); // smtpcorp.com

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue:
while(true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// you’re connected now, so print out the data:
Serial.print(“You’re connected to the network”);
printCurrentNet();
printWifiData();

}

void loop() {
// check the network connection once every 10 seconds:
delay(10000);
printCurrentNet();

// Check for serial input
byte inChar;

inChar = Serial.read();

if (inChar == ‘e’)
{
if(sendEmail()) {
Serial.println(“Email sent”);
}
else {
Serial.println(“Email failed”);
}
}

}

// Andy’s code
byte sendEmail() {
byte thisByte = 0;
byte respCode;

if (client.connect(server,2525)) {
Serial.println(“connected”);
} else {
Serial.println(“connection failed”);
return 0;
}

if(!eRcv()) return 0;

// EHLO
//client.print(“EHLO\r\n”); //Problem starts here

client.println(F(“EHLO\r\n”));
if(!eRcv()) return 0;

//client.print(“AUTH LOGIN\r\n”);
client.println(F(“AUTH LOGIN\r\n”));

if(!eRcv()) return 0;
client.println (F("****************************\r\n"));

if(!eRcv()) return 0;
client.println (F("*********\r\n"));

if(!eRcv()) return 0;

// change this
client.print(F(“MAIL From: qianmiao517@gmail.com\r\n”));

if(!eRcv()) return 0;

// change this
client.print(F(“RCPT To: jiajia199304@gmail.com\r\n”));

if(!eRcv()) return 0;

client.print(F(“DATA\r\n”));

if(!eRcv()) return 0;

//change this
client.print(F(“To: You jiajia199304@gmail.com\r\n”));

// change this
client.print(F(“From: Me qianmiao517@gmail.com\r\n”));

client.print(F(“Subject: Arduino email test\r\n”));

client.print(F(“Suck it\r\n”));

client.print(F(".\r\n"));

if(!eRcv()) return 0;

client.print(F(“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”);
}

void printWifiData() {
// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);

// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print(“MAC address: “);
Serial.print(mac[5],HEX);
Serial.print(”:”);
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);

}

void printCurrentNet() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you’re attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print(“BSSID: “);
Serial.print(bssid[5],HEX);
Serial.print(”:”);
Serial.print(bssid[4],HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print(“Encryption Type:”);
Serial.println(encryption,HEX);
Serial.println();
}

this is my serial monitor :

220 smtpcorp.com Esmtp Exim 4.85 Sun,03 May 2015 250-smtpcorp.com.Hello [14..100.132.123] 250-SIZE 52428800 250-8BIMIME 250-AUTH CRAM-MD5 PLAIN LOGIN 250-STARTTLS

250HELP 500 unrecognized command 334 VXN1 cm5hbwu6 334 UGFzc3dvcmQ6 501 INVALID BASE 64 DATA 500 unrecognized command 221 smtpcorp.com closing connection disconnected Email failed

Maybe you are sending too many \r\n for the email server. Note one commented out is a client.print, not client.println.

 //client.print("EHLO\r\n");    //Problem starts here

client.println(F("EHLO\r\n"));
 if(!eRcv()) return 0;

Here is the sendEmail function I just tested with smtp2go. You must change the “xxxx” and “yyyy” to your base64 user and password. You can do that here. Use UTF-8 character set.

Also change the to and from email addresses in both places specified by the comments.

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;

  if(client.connect(server,2525) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;
  Serial.println(F("Sending helo"));

  client.println("EHLO");

  if(!eRcv()) return 0;

  client.println("auth login");

  if(!eRcv()) return 0;

// change this to your base64 user
  client.println("xxxx");

  if(!eRcv()) return 0;

//' change this to your base64 password
  client.println("yyyy");

  if(!eRcv()) return 0;

  Serial.println("Sending From");

// change to your email address (sender)
  client.println("MAIL From: <me@mydomain.com>");

  if(!eRcv()) return 0;

// change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT To: <you@yourdomain.com>");

  if(!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  client.println("DATA");

  if(!eRcv()) return 0;

  Serial.println(F("Sending email"));

// change to recipient address
  client.println("To: You <you@yourdomain.com>");

// change to your address
  client.println("From: Me <me@mydomain.com>");

  client.println("Subject: Arduino email test\r\n");

  client.println("This is from my Arduino!");

  client.println(".");

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  client.println("QUIT");

  if(!eRcv()) return 0;

  client.stop();

  Serial.println(F("disconnected"));

  return 1;
}

Here is the serial monitor output with the sender IP and address changed.

Ready. Press 'e' to send.
connected
220 smtpcorp.com ESMTP Exim 4.85 Thu, 07 May 2015 20:46:51 +0000
Sending helo
250-smtpcorp.com Hello  [1.2.3.4]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH CRAM-MD5 PLAIN LOGIN
250-STARTTLS
250 HELP
334 VXNlcm5hbWU6
334 UGFzc3dvcmQ6
235 Authentication succeeded
Sending From
250 OK
Sending To
250 Accepted <you@yourdomain.com>
Sending DATA
354 Enter message, ending with "." on a line by itself
Sending email
250 OK id=1YqSh2-NRKGuk-9u
Sending QUIT
221 smtpcorp.com closing connection
disconnected
Email sent