Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: koklimabc on Oct 12, 2012, 10:52 am

Title: How to access smtp.gmail.com or other to send email
Post by: koklimabc on Oct 12, 2012, 10:52 am
Can anyone help me on how to access to gmail smtp server and make changes to the following. Appreciate thanks to any suggestion and help.

I've tried my following setup code without any problem to send out email automatically to my gmail mailbox under my local smtp domain server.

My main purpose would liked to use public smtp server for sending out email but not involved local setup smtp mail's server because I'll create a device which is not rely on local setup email server but connect solely direct to Internet for automatically send out email by interact to public smtp server only.



#include <Ethernet.h>

#include <SPI.h>  

//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//byte ip[] = { 192, 168, 9, 177 };
//byte server[] = { 192, 168, 9, 6 }; // Mail server address  MODIFY THIS FOR THE TARGET DOMAIN's MAIL SERVER

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 9, 177 }; // ip in lan
byte gateway[] = { 192, 168, 9, 3 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte smtp[] = { 192,168,9,6 };

EthernetClient client;
//EthernetServer server(25);
//Client client(server, 25);
//Client client;
void setup()
{
 Ethernet.begin(mac, ip,gateway,subnet);
 //server.begin();
 Serial.begin(9600);
 
 delay(1000);
 
 Serial.println("connecting...");
 
// EthernetClient client = server.available();
 
 if (client.connect(smtp,25)) {
   Serial.println("connected");
   client.println("EHLO kltan");
   client.println("AUTH LOGIN");                         //see "http://base64-encoder-online.waraxe.us/"
   client.println("a2x0YW5AcHBoLmNvbS5teQ==");           //Type kltan@pph.com.my and encode it
   client.println("MTIzNDU2");                                                      //This line is password
   client.println("MAIL FROM:<someone@hotmail.com>");  
   client.println("RCPT TO:<someone@gmail.com>");
   client.println("DATA");
   client.println("from:someone@hotmail.com");
   client.println("to:someone@gmail.com");
   client.println("SUBJECT: Testing subject to arduino ethernet shield");
   client.println();
   client.println("This is the line body.");
   client.println("This is another line of the body.");
   client.println(".");
   client.println(".");
   client.println("QUIT");
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 ;;
   if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   for( ;; )
    ;
  }
}
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Oct 12, 2012, 01:41 pm
This link has the code I use.
http://arduino.cc/forum/index.php/topic,104449.msg784053.html#msg784053

It reads and displays the email server response after each send so you will know if it is working, and why if not.

edit: If you do not have a commercial account with a static ip, you may be blocked by the gmail server.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: koklimabc on Oct 12, 2012, 06:53 pm
I've already tried your previous post coding test, but it still happened to have the same problem where I can't pass through STARTTLS.
How can i do to pass over STARTTLS?
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Oct 12, 2012, 07:32 pm
The ethernet library does not support TLS. You can't send a user and password.

If you are using port 25, it should not want to start TLS. It does not need a user/password. Gmail is a bit picky about the sender ip range. Try using nslookup to resolve the correct server ip for your area.

Title: Re: How to access smtp.gmail.com or other to send email
Post by: sbright33 on Oct 14, 2012, 04:24 pm
You can use this instead of Gmail, it's Free:
http://www.smtp2go.com/
Title: Re: How to access smtp.gmail.com or other to send email
Post by: Murmlgrmpf on Dec 09, 2012, 09:42 pm
Thank you so much! The following steps got me sending emails via my arduino and a wifly ethernet shield when the washing machine has finished:
register on http://www.smtp2go.com/
get the encoded version of your username and password from http://base64-encoder-online.waraxe.us/ or any other base64 encoder you prefer.
Then alter the web client example to send an email as shown above.

regards

Thomas
Title: Re: How to access smtp.gmail.com or other to send email
Post by: sbright33 on Dec 10, 2012, 09:46 pm
That's what I did too, works perfectly every time.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: EJTR on Mar 29, 2013, 11:08 pm

hi , I am testing your propose but when you try from cmd command ( telnet port 2525 ) with an account smtp2go do not works.

thanks for your comments
Title: Re: How to access smtp.gmail.com or other to send email
Post by: zoomkat on Mar 30, 2013, 02:16 am


hi , I am testing your propose but when you try from cmd command ( telnet port 2525 ) with an account smtp2go do not works.

thanks for your comments


sounds like a replay of the below:

http://arduino.cc/forum/index.php/topic,134420.msg1010872.html#msg1010872
Title: Re: How to access smtp.gmail.com or other to send email
Post by: EJTR on Apr 02, 2013, 12:22 am

I followed your advice , I have an email account in smtp2go and this is my code but it do not works even not connects. I tested with cmd command from my pc with my smtp2go account and I had not a problem but with arduino does no connect.

thanks for your comments and sorry my english is not good.

Code: [Select]

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x5C, 0x16};
IPAddress ip(192,168,1,5);
byte gateway  [] = {192,168,1,1};
byte subnet   [] = {255, 255, 255, 0};
byte smtp[] = {186,47,174,186};//{ 207,58,147,66 }; // I tried with two ip
EthernetServer server(80);                          //nslookup to smtpcorp.com

//IPAddress server(207,58,147,66); // conflicting declaration 'IPAddress server' -- and I do not why.
EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip,gateway,subnet);
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");
  if (client.connect(smtp,8025)) //2525/25/8025/587/465/8465 // I tried with all ports
  {
    delay(500);
    client.println("ehlo");
    delay(500);
    client.println("auth login");     
    delay(500);
    client.println(" name account base 64");  //mail account with smtp2go         
    delay(500);
    client.println("password base 64");
    delay(500);   
    client.println("mail from: mail account with gmail");  //This is related to smtp2go
    delay(500);
    client.println("rcpt to: xxxxxxxxxx");
    delay(500);
    client.println("data");
    delay(500);
    client.println("To: xxxxx");
    delay(500);
    client.println("From: xxxxx");
    delay(500);
    client.println("Subject: MENSAJE DE PRUEBA");
    delay(500);
    client.println();
    delay(500);
    client.println("ESTE ES UN MENSAJE DE PRUEBA DESDE ARDUINO");
    delay(500);
    client.println(".");
    delay(500);
    client.println("quit");
  }
  else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar);
    }
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    while(true);
  }
}


Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Apr 02, 2013, 01:02 am
I will point out one the first of many problems with this code. This is incorrect.
Code: [Select]
  Ethernet.begin(mac, ip,gateway,subnet);

http://arduino.cc/en/Reference/EthernetBegin

Of course, you won't know why the rest fails. You are not reading the response from the email server. I posted a link to the code I use a few posts ago.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: katonafull on Oct 18, 2016, 11:40 am
Thank you so much! The following steps got me sending emails via my arduino and a wifly ethernet shield when the washing machine has finished:
register on http://www.smtp2go.com/
get the encoded version of your username and password from http://base64-encoder-online.waraxe.us/ or any other base64 encoder you prefer.
Then alter the web client example to send an email as shown above.

regards

Thomas
Could you share your code please?

Thx!
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Oct 18, 2016, 02:15 pm
It is the second example on this page.
http://playground.arduino.cc/Code/Email (http://playground.arduino.cc/Code/Email)
Title: Re: How to access smtp.gmail.com or other to send email
Post by: katonafull on Oct 20, 2016, 03:22 pm
It is the second example on this page.
http://playground.arduino.cc/Code/Email (http://playground.arduino.cc/Code/Email)
Thank You!
I found some examples in the meantime.
The examples use the
Code: [Select]
char server[] = "smtpcorp.com";
 smtp adress. But I saw the webpage of the gmail that is about the smtp. https://support.google.com/a/answer/176600?hl=en&ref_topic=1354753 (https://support.google.com/a/answer/176600?hl=en&ref_topic=1354753) And it doesn't contain "smtpcorp.com" adress. It mentioned 3 possibiliy 1.: "smtp-relay.gmail.com" 2.: "smtp.gmail.com" 3.: "aspmx.l.google.com". Should I change the smtpcorp.com to one of them?
And now I find a webpage that say use mail.smtp2go.com like SMTP server. https://support.smtp2go.com/hc/en-gb/articles/223087627-SMTP-Server-Settings (https://support.smtp2go.com/hc/en-gb/articles/223087627-SMTP-Server-Settings) What the hell?
I will try this: mail.smtp2go.com  :)
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Oct 20, 2016, 03:36 pm
You can't send through Gmail. It requires security that the Wiznet ICs and libraries do not support.

SmtpToGo is the best alternative. You must use smtpcorp.corp as the email server domain name of you will not be able to connect. I just tried it and it works.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: katonafull on Oct 20, 2016, 04:46 pm
I found another sketch.
http://www.jucetechnology.com/wordpress/simple-burglar-ethernet-shield-connection/ (http://www.jucetechnology.com/wordpress/simple-burglar-ethernet-shield-connection/) Why don't have to use it IPAddress, IPAddress gateway, and IPAddress subnet ? And what is the deficiency of this example? Why is it much easier than your example? Ex cuse me for my questions! I would like to understand the differences.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: appliedpr on Dec 20, 2016, 04:17 am
katonafull,

I have been following your dialog for some time, now.  The reason being that I have exactly your same problem.  Today, I was able for the first time to communicate and send my first email using the simple burglar alarm sketch that you mention. I just wanted to thank you, because I was not successful with your referenced topic either.

The only changes that have to be made to the burglar alarm sketch is to limit to one or two emails per event because as is, it sends a train of emails without holding to comply with the SMTP2GO server limits.

Thank you again.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Dec 20, 2016, 01:58 pm
The "simple" burglar alarm sketch does no error checking. It retrieves the response but doesn't display it. If something goes wrong, it is almost impossible to determine where the problem is.

edit: It simple sketch will also return success even if the send failed for any reason, like user/password incorrect, TO or FROM address errors, etc.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: sandyproject21 on Jun 02, 2017, 10:28 am
I compile the codes and there is no error.
I run the serial monitor. Connection OK but email failed.

What can I do to test email send from arduino? I am using the ethernet shield W5100.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Jun 02, 2017, 02:08 pm
If your email client sketch had error checking, you would know why it failed. This example in the playground has error checking.
http://playground.arduino.cc/Code/Email (http://playground.arduino.cc/Code/Email)
Title: Re: How to access smtp.gmail.com or other to send email
Post by: hanluu on Jan 26, 2018, 04:07 pm
Hi surfertim

the smtp2go user is the gmail( hanluu@gmail.com) address or just user name, because i saw my smtp2go account show the user name (hanluu).thanks
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Jan 26, 2018, 04:30 pm
I use the email address I registered with, but it must be base64 encoded.
https://www.base64encode.org/ (https://www.base64encode.org/)
Same with the password. Base64 encoded.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: hanluu on Jan 26, 2018, 06:47 pm
email sent.
 thank you very much
i just change my user name ''hanluu'' into email address then BOOM. I GOT MAIL. THANKS AGAIN.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: hanluu on Jan 26, 2018, 08:10 pm
can you show me how to add a float switch to trigger the email? thank you.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Jan 27, 2018, 12:12 am
I suppose you got the code here.
http://playground.arduino.cc/Code/Email (http://playground.arduino.cc/Code/Email)


If so, in the second example at line 37
Code: [Select]
// replace this
  if(inChar == 'e')

// with something like this
  if(triggerPin == HIGH)
Title: Re: How to access smtp.gmail.com or other to send email
Post by: hanluu on Jan 27, 2018, 07:56 am
Hi.

Can you fix this code to send only 1 email when it trigger the sensor,right now when sensor trigger it send alot of mail. thank you.


Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
int LED = 13;                // choose the pin for the LED
int SENSOR = 2;               // choose the input pin (for SENSOR sensor)
int VAL =digitalRead(SENSOR);                    // variable for reading the pin status
int SENSORS = LOW;           

// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };
// change network settings to yours
IPAddress ip( 192, 168, 0, 32 );
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );

char server[] = "mail.smtp2go.com";
int port = 2525; // You can also try using Port Number 25, 8025 or 587.

File myFile;

EthernetClient client;

void setup()
{

  Serial.begin(9600);
  pinMode(LED, OUTPUT);      // declare LED as output
  pinMode(SENSOR, INPUT);     // declare sensor as input
  digitalWrite(LED, LOW);
 
Ethernet.begin(mac);
delay(2000);
Serial.println(Ethernet.localIP());
Serial.println(F("Ready. Press 'e' to send."));

}


void loop(){

 if (digitalRead(SENSOR) == HIGH) {           
    digitalWrite(LED, HIGH);  // turn LED ON
  ////delay (300);
   
    if (SENSORS == LOW) {
      Serial.println("Motion detected!");
    SENSORS = HIGH;
    }
  }
  else {
      digitalWrite(LED, LOW); // turn LED OFF
 
      if (SENSORS == HIGH){
      Serial.println("Motion ended!");
      SENSORS = LOW;
    }
  }
 
byte inChar;

inChar =  digitalRead(SENSOR);               /// Serial.read(); 

if(inChar == HIGH  )
{
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) == 1) {
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 192.168.0.31 ");
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(F("yyyyyyyyyyyyyyyyy"));


if(!eRcv()) return 0;

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


if(!eRcv()) return 0;

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

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

// change to your address
client.println("From: <hanluu@hotmail.com>");

client.println("Subject: Your Subject");

client.println("Hi! Simple test message");

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"));
}



Title: Re: How to access smtp.gmail.com or other to send email
Post by: SurferTim on Jan 27, 2018, 01:03 pm
Take a look at the "BlinkWithoutDelay" example included with the IDE. That should give you a way to prevent multiple sends.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: yash023 on Feb 28, 2018, 08:13 am
Hello,

Anyone can help me how to data sent to our Gmail account using Arduino and ESP8266(temperature and humidity) could u please send an anyone a sample code.
Title: Re: How to access smtp.gmail.com or other to send email
Post by: TimG123 on Nov 19, 2018, 10:16 am
This is a very useful thread, thanks to all.  I got stuck with an error message saying that I wasn't allowed to send messages to SMTP2GO as I didn't have a senders domain.  This is apparently a new security feature of SMTP2GO.  Here's the steps to take to get it working:

1. Create account at https://www.smtp2go.com/
2. Add your email address as a user. Note that each user has their own passwords (separate to the admin account created above).
3. To get around the recent issue of needing to have your own sender domain, which is a problem if you want to use say a gmail or ISP issued email address: Make contact with SMTP2GO Support and explain your use case, and they will adjust your account appropriately. This way you can keep Sender Domains blank. If you try and enter a sender domain that you don't own, you'll receive the error message "The domain you have entered is not allowed. Please note that you must have control of the DNS of any domain that you add." (To check if this issue is occurring, open the serial monitor before code execution, and after "235 Authentication Succeeded" you will see an error "That user is not allowed to send").
4. Restrict senders to your chosen email addresses - this provides some level of security against spammers.
5. Incorporate the code at https://www.smtp2go.com/setupguide/arduino/  into your Arduino sketch.
6. The username and password must be sent using base64 encoding by the Arduino to SMTP2GO. You can use https://www.base64encode.org/ (amongst other sites) to do the encodes. (To check if you got the password right, open the serial monitor before code execution and look for "Sending Password".  The next line should say "235 Authentication succeeded".  Otherwise, check you've got the right password and done the base64 encoding correctly).
Title: Re: How to access smtp.gmail.com or other to send email
Post by: samsingania44 on Jan 25, 2019, 08:21 am
Gmail Customer Service Number+1833-222-2937

Is an especially open and shrewd stage. Regardless, it without question can have numerous segments that can be really perplexing for the customers. Make an effort not to stress, at Gmail Customer Service number, we outfit you with a 24×7 help concerning finding answers for a wide scope of your Gmail disorders. gmail phone number, We offer 'Constant" advantage with our specific push utilities to our customers for their straightforwardness and solace. gmail customer care number +1833-222-2937