Sending email with Arduino

Hello,

I am working on a project where i want to send a mail when an avent happens. This is my code, but it doesn't send the mail and i don't know why..

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

// Change these next 4 entries to your network settings
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char server[] = "smtp.ziggo.nl"; //smtp server ip address
EthernetClient client;

int button1 = 2; //button pin, connect to ground to move servo
int press1 = 0;

void setup()
{
  Serial.begin(9600);
  delay(10);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting to network...");

  
  pinMode(button1, INPUT);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  
  Serial.println("Ready");
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  press1 = digitalRead(button1);
     
  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Serial Email sent"); //start sendEmail()
    else Serial.println("Serial Email failed");
  }
 
   if (press1 == HIGH)
  {
    delay(2000);
    if(sendEmail()) Serial.println("Button Email sent"); //start sendEmail()
    else Serial.println("Button Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(server,25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }
  
  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO Arduino"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: somebody@gmail.com"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: somebody@gmail.com"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: somebody@gmail.com"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
  
  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

Anybody knows what i am doing wrong?

Thanks in advance,

Jan

Event based email test code.

//zoomkat 4/08/12
//email test code using DNS
//developed from code posted by various persons

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

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "smtp.yourISP.net"; // your ISP's SMTP server
EthernetClient client;

//////////////////////

void setup(){
  Serial.begin(9600); 
  Serial.println("DNS and DHCP-based email test 4/08/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // print your local IP address:
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  Serial.println();
}

void loop()
{
  byte inChar;
  inChar = Serial.read();
  if(inChar == 'e')
  {
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
    else Serial.println("Email failed");
  }
}

//////////////////////////////// email function
byte sendEmail()  //sendEmail() function that returns 1 or 0
{
  //start and check for email server connection
  if (client.connect(serverName, 25)) { 
    Serial.println("connected");
  } 
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  }
  
  //wait for server "queing" response
  while(!client.available()) delay(1);

  client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1);

  client.println("MAIL From: me@athome.net"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1);

  client.println("RCPT To: you@athome.net"); /* identify recipient */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1);

  client.println("DATA"); 

  //wait for server to say "enter your message" response
  while(!client.available()) delay(1);
 
  //send email message to server
  client.println("To: you@athome.net"); /* identify recipient */
  client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
  client.println("Please let me know it worked!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1);

  client.println("QUIT"); /* terminate connection */
  
  //wait for server "goodby" response
  while(!client.available()) delay(1);

  //stop client connection
  client.stop();
  Serial.println("disconnected");
  return 1; //send 1 (success) back to sendEmail() function
}

janverhoeckx:
it doesn't send the mail

Could you elaborate on this? You've got some debug statements in there that should tell you whether it tried to send the email and whether it succeeded. What do they tell you?

Here is an email sketch that actually displays what the email server is returning.
http://playground.arduino.cc/Code/Email
That may help you figure out why it is not working. Here is an example serial monitor display for a successful send.

Ready. Press 'e' to send.
connected
220 mail.yourmailserver.com ESMTP Sendmail 8.13.6/8.13.1; Sun, 17 Mar 2013 12:59:58 +0000
Sending helo
250 mail.yourmailserver.com Hello [1.2.3.4], pleased to meet you
Sending From
250 2.1.0 me@mine.com... Sender ok
Sending To
250 2.1.5 you@mine.com... Recipient ok
Sending DATA
354 Enter mail, end with "." on a line by itself
Sending email
250 2.0.0 r2HCxwgc027560 Message accepted for delivery
Sending QUIT
221 2.0.0 mail.yourmailserver.com closing connection
disconnected
Email sent