SMTP Email Problem

Yesterday at home i tried sending an email with arduino uno and ethernetshield and it was okay! But now i am trying to do exactly the same thing at work but it is not working! Everything is same i just change the port because it seems 2525 is closed at company’s network but 25 is open. I don’t understand much about network problems so if there is anybody who knows the answer please help.

The code I use is given below

/*
   Email client sketch for IDE v1.0.5 and w5100/w5200
   Posted 7 May 2015 by SurferTim
*/

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

// this must be unique
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };  
// change network settings to yours
IPAddress ip( 192, 168, 0, 28 );    
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );

char server[] = "mail.smtp2go.com";
int port = 25;

EthernetClient client;

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

void loop()
{
  byte inChar;

  inChar = Serial.read();

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

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

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

  if(!eRcv()) return 0;

  Serial.println(F("Sending hello"));
// replace 1.2.3.4 with your Arduino's ip
  client.println("EHLO 85.105.81.42");
  if(!eRcv()) return 0;

  Serial.println(F("Sending auth login"));
  client.println("auth login");
  if(!eRcv()) return 0;

  Serial.println(F("Sending User"));
// Change to your base64 encoded user
  client.println("xxxx");     //my user name

  if(!eRcv()) return 0;

  Serial.println(F("Sending Password"));
// change to your base64 encoded password
  client.println("yyyy");     //password

  if(!eRcv()) return 0;

// change to your email address (sender)
  Serial.println(F("Sending From"));
  client.println("MAIL From: <me@gmail.com>");
  if(!eRcv()) return 0;

// change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT To: <@someonegmail.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 <someone@gmail.com>");

// change to your address
  client.println("From: Me <me@gmail.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;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while(!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }

  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;
  int loopCount = 0;

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

  while(!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return;
    }
  }

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

  client.stop();

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

And my serial monitor shows

Ready. Press ‘e’ to send.
connected

Timeout
Email failed

So what is the problem?

IPAddress ip( 192, 168, 0, 28 );    
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );

This is a classic home configuration, I doubt it's the way your company setup looks like. You have to change that to a setting that is valid in your company.

So you should ask your administrator for an ip address to use and the basic settings. If that's a problem (eventually the arduino is not part of a company project ;)), don't connect your arduino to the company network directly.

It depends on the company your working for...

Thanks a lot! But there was no problem about that and i finally succeed!! :D I found an information it was written

"Some devices (fax machines etc.) may benefit from using the IP address of your SMTP server instead of its name (i.e. 207.58.147.66 if your SMTP server is smtpcorp.com, or 207.58.142.213 if your SMTP server is smtp2go.com)."

so i put IPAddress server (207,58,142,213); instead of char server[] = "mail.smtp2go.com"; and that's it!

That means your dns resolution was failing. You are using an old copy of my sketch. It probably needs a dns server specified in your Ethernet.begin() call. My router will perform dns resolution. The router at your company probably does not.

// change this
  if(client.connect(server,port)) {
// to this
  if(client.connect(server,port) == 1) {

If I change if(client.connect(server,port)) to if(client.connect(server,port) == 1) it is not working so i did delete the equality and now it is working… actually I found it weird aren’t they mean the same thing? :cold_sweat:

They are not the same. The client.connect() call returns 1 on success, 0 if unable to connect to the server, and anything from -1 to -10 if the dns resolution fails. The -1 to -10 return values evaluate to true (actually "not false" to be correct).

This is the indication that the dns resolution failed. It shows connected (incorrectly) and times out.

Ready. Press 'e' to send. connected

Timeout Email failed

I got it! I changed it like you said it worked but sometimes it says connection failed it seems like it is because of network so i need to contact IT

Thanks a lot!!

You might want to evaluate the return value from the connect function to see if it is the dns resolution or the connect attempt that is failing.

  int rtnVal = client.connect(server,port);

  switch(rtnVal) {
    case 1: Serial.println("connected");
                break;

    case 0: Serial.println("connection failed");
                return 0;

    default: Serial.print("dns failed. value: ");
                Serial.println(rtnVal);
                return 0;
  }