Hello, I've been messing around with this code for a couple of days now and can't seem to make a connection with the server, I constantly get:
connection failed
Email failed
here's the code I've copied from your posts:
what do you think the connection problems could be? I do see the link light on when I plug the ethernet cable into the shield. I've pinged smtp.gmail.com for an ip and it still doesn't connect.
Thanks!
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x26, 0x38 };
IPAddress ip( 192, 168, 1, 21 );
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );
// gmail.com email server
IPAddress server( 173, 194, 69, 26 );//( 173, 194, 69, 26 )
EthernetClient client;
void setup()
{
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(10000);
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 67.231.68.89\r\n");
if(!eRcv()) return 0;
// change this
client.write("MAIL From: <mphgardner@gmail.com>\r\n");
if(!eRcv()) return 0;
// change this
client.write("RCPT To: <mphgardner@gmail.com>\r\n");
if(!eRcv()) return 0;
client.write("DATA\r\n");
if(!eRcv()) return 0;
//change this
client.write("To: You <mphgardner@gmail.com>\r\n");
// change this
client.write("From: Me <mphgardner@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");
}