TCPclient connect with authentication to SMTP server

Gotta a little code that when a button is pressed, it connects to a SMTP server using Client.connect function and with basic SMTP commands, sends an email. Everything is working A1 except now I am moving the device in a different location and their SMTP server need auth login. No problem with my Arduino but cannot make it work with my spark… no clues… When the button is pressed, it start the routine but never ends… here’s a sample code example… If I // the bold area, it works, if I uncomment the bold it dies… Anybody have a clue?

if (buttonState2 == HIGH) {
digitalWrite(ledready, LOW);
digitalWrite(beep1, HIGH);
delay(200);
digitalWrite(beep1, LOW);
i=57;
client.connect(server, 25);
delay(1000);
client.println(“auth login”);
client.println(“dmx2cWFtb2c=”);
client.println(“passwordchangedforobviousreason”);

client.println(“mail from: john@doe.com”);
client.println(“rcpt to: you@you.com”);
client.println(“data”);
client.println(“This is line 1”);
client.println(“This is line 2”);
client.println(".");
client.flush();
delay(10000);
digitalWrite(beep1, HIGH);
delay(200);
digitalWrite(beep1, LOW);
delay(50);
digitalWrite(beep1, HIGH);
delay(200);
digitalWrite(beep1, LOW);

}

The 10 second delay is going to be a problem for you… it blocks the background tasks on the Spark Core and it will disconnect from the cloud…

Try creating a state machine that runs you through each step, but then drops out to continue through the main loop() once, then onto the next task. :smile:

int sendMail = false;
int sendMailState = 0;
int wait1 = 2000;
int wait2 = 2000; // play with these delays

setup() {
  // setup your outputs...
}

loop() {

  if (buttonState2 == HIGH && sendMail == false) { 
    digitalWrite(ledready, LOW);
    digitalWrite(beep1, HIGH);
    delay(200);
    digitalWrite(beep1, LOW);
    sendMail = true;
    sendMailState = 0;
  }

  if( sendMail == true ) {
    switch( sendMailState++ ) {
      case 0:
        client.connect(server, 25);
        delay(wait1);
        break;
      case 1:
        client.println("auth login");
        delay(wait2);
        break;
      case 2:
        client.println("dmx2cWFtb2c=");
        delay(wait2);
        break;
      case 4:
        client.println("passwordchangedforobviousreason");
        delay(wait2);
        break;
      case 5:
        client.println("mail from: john@doe.com");
        delay(wait2);
        break;
      case 6:
        client.println("rcpt to: you@you.com");
        delay(wait2);
        break;
      case 7:
        client.println("data");
        client.println("This is line 1");
        client.println("This is line 2");
        client.println(".");
        client.flush();
        delay(wait);
        break;
      case 8:
        digitalWrite(beep1, HIGH);
        delay(200);
        digitalWrite(beep1, LOW);
        delay(50);
        digitalWrite(beep1, HIGH);
        delay(200);
        digitalWrite(beep1, LOW);
        sendMail = false;
        break;
      default:
        sendMail = false;
        break;      
    } // end switch
  } // end send mail
} // end loop()
1 Like

resolve my problem… added a client.flush after each command line in smtp auth…
client.println(“ehlo”);
client.flush();
client.println(“auth login”);
client.flush();
client.println(smtpuser);
client.flush();
client.println(smtppass);
client.flush();
client.println(emailexpediteur);
client.println(emaildestingroupe);

works A1

2 Likes

Hmm… now you got me wondering why the heck that works. Flush() just read()'s until there available() == false. Can’t see how that’s helping… if you are even reading anything.

something happened in the last days with the web API cause I ref lash my core and with the same code, nothing works anymore… Just sending an email is impossible with a press of a button… Cancelled my orders for 4 other cores… Going back to arduino to finish my project…

I tried you’re program DBub, when I try to flash the program it comes up with:

the_user_app.cpp:23:9: error: ‘client’ was not declared in this scope
}
^
the_user_app.cpp:23:24: error: ‘server’ was not declared in this scope
}
^
the_user_app.cpp:52:15: error: ‘wait’ was not declared in this scope
client.println(“data”);