Recent changes
Slingshot user guide
tag Guide, user
NFCLamp user guide
tag Guide, user
Homepage
MPL115A2
Compiler Error 42
From the mbed microcontroller Cookbook.  

SMTPClient

This documentation is for the SMTPClient class in the Segundo Equipo version of the NetServices library. To be used in conjunction with the EmailMessage class.

To test, import the test program into the compiler and then edit the #define items:

Code

#define SERVER "server"
#define PORT 25
#define USER "username"
#define PASSWORD "password"
#define DOMAIN "mailnull.com"
#define FROM_ADDRESS "segundo.smtpclient@mailnull.com"
#define TO_ADDRESS "segundo.smtpclient@mailnull.com"

Authentication and encryption

The SMTPClient supports plain authentication. This means that the username and password are encoded (but not encrypted) into base 64 for sending to the server.

The SMTPClient does not support encryption of the authentication credentials or the message content. The class will therefore not be suitable for use with an SMTP server that enforces use of, for example, SSL or TLS.

» Import this library into a program

Public Member Functions

SMTPClient ()
Instantiates the SMTP client.
virtual ~SMTPClient ()
Destructor for the SMTP client.
SMTPClient (const Host &host, const char *heloDomain, const char *user, const char *password, SMTPAuth auth)
Full constructor for the SMTP client.
void setServer (const Host &host)
Set server host.
void setAuth (const char *user, const char *password)
Provides a plain authentication feature (Base64 encoded username and password)
void clearAuth ()
Turns off authentication.
void setHeloDomain (const char *heloDomain)
Set HELO domain (defaults to localhost if not set)
SMTPResult send ( EmailMessage *pMessage)
Sends the message (blocking)
SMTPResult send ( EmailMessage *pMessage, void(*pMethod)( SMTPResult ))
Sends the message (non blocking)
template<class T >
SMTPResult send ( EmailMessage *pMessage, T *pItem, void(T::*pMethod)( SMTPResult ))
Sends the message (non blocking)
void doSend ( EmailMessage *pMessage)
Sends the message (non blocking)
void setOnResult (void(*pMethod)( SMTPResult ))
Setup the result callback.
void setTimeout (int ms)
Setup timeout.
string & getLastResponse ()
Gets the last response from the server.
virtual void poll ()
This method can be inherited so that it is called on each Net::poll() call.

Protected Member Functions

void close ()
This flags the service as to be destructed if owned by the pool.

» Import this library into a program

Public Member Functions

EmailMessage ()
Instantiates the email message.
~EmailMessage ()
Destructor for the email message.
void setFrom (const char *from)
Set FROM address.
void addTo (const char *to)
Add TO address to list of recipient addresses.
void clearTo ()
Clear TO addresses.
int printf (const char *format,...)
Append text to content of message using printf.
void clearContent ()
Clear content previously appended by printf.

Friends

class SMTPClient



calendar Page history
Last modified 28 Nov 2010, by   user Daniel Peter   tag email, SMTP | 26 comments  

26 comments on SMTPClient:

17 Jan 2011

I was looking for a host that didn't require SSL or TLS but I didn't see one. Do you know what hosts this will work for?

Thanks, Ben

04 Mar 2011

I can almost get though but keep running into a:

"Last response | 504 5.7.4 Unrecognized authentication type" error. In over my head on that one.

Suggestions appreciated.

04 Mar 2011

Quote:

I was looking for a host that didn't require SSL or TLS but I didn't see one. Do you know what hosts this will work for?

Thanks, Ben

Hi Ben

I tested this with authsmtp.com and also the o2.co.uk mailserver.

Regards Daniel

04 Mar 2011

Quote:

I can almost get though but keep running into a:

"Last response | 504 5.7.4 Unrecognized authentication type" error. In over my head on that one.

Suggestions appreciated.

Hi Steve

It's possible the authentication required by the mailserver is not supported by this simple SMTP client. What server are you attempting to use? Can you provide an address and I'll telnet to it to check it out?

Regards Daniel

24 Mar 2011

used SMTPClient_HelloWorld in a program that makes a measurement then tweets the results in a while loop. After the tweet the programs goes back to the top of the while loop and waits for the next time for measurement, makes the measurement, then tweets the new measurement. I added this so I can email the result after a successful tweet. The program works for the first measurement, both tweet and email is successful. But after the first measurement, tweet, and email the program hangs up at the top of the while loop. I then took SMTPClient_HelloWorld itself and made it loop back to near the beginning after the email is sent to see if the same thing happens. After the first email is sent the program re-establishes the ethernet connection but hangs up when it tries to access NTP.

Need help!

25 Mar 2011

Hi Dan

I'm not going to be able to look at this for a week. Can you publish your code anyway, so in the meantime somebody else might help? I'll take a look as soon as I'm back.

Regards Daniel

26 Mar 2011

/media/uploads/phase/main_copy.txt Here is my modification of SMTPClient_HelloWorld that I loop back using goto stub; The email is sent successfully the first time thru the program, the goto stub; statement is executed and goes back to stub: The ethernet connection is made again, IP and Hardware address is printed the program then hangs up at printf("NTP setTime...\n"); NTP setTime... is printed on the terminal and then hangs up.

26 Mar 2011

thanks in advanced for any help here is the listing again:

  1. include "mbed.h"
  2. include "EthernetNetIf.h"
  3. include "NTPClient.h"
  4. include "SMTPClient.h"
  1. define HOSTNAME "mbedSE"

server is domain name eg. mail.authsmtp.com port is smtp port eg. 25 or 23 domain must be acceptable to server (but often is not strictly enforced since does not affect FROM address)

  1. define SERVER "server"
  2. define PORT 587
  3. define USER "username"
  4. define PASSWORD "password"
  5. define DOMAIN "domain"
  6. define FROM_ADDRESS "address@gmail.com"
  7. define TO_ADDRESS "address@gmail.com"

const char* bufshallow[9]={"zero","moist","wet","three","vry dry","dry", "opnshrt","seven","setup"

}; const char* bufdeep[9]={"zero","moist","wet","three","vry dry","dry", "opnshrt","seven","setup" }; const char* bufvalve[5]={"zero","disabled","enabled","three","setup"}; int vs=1,vd=1,vv=1;

int main() {

printf("\n\n/* Segundo Equipo SMTPClient library demonstration */\n"); printf("Make sure you have modified the SERVER, PORT etc in the source code\n"); stub: EthernetNetIf eth(HOSTNAME); EthernetErr ethErr; int count = 0; do { printf("Setting up %d...\n", ++count); ethErr = eth.setup(); if (ethErr) printf("Timeout\n", ethErr); } while (ethErr != ETH_OK);

printf("Connected OK\n");

const char* hwAddr = eth.getHwAddr(); printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n", hwAddr[0], hwAddr[1], hwAddr[2], hwAddr[3], hwAddr[4], hwAddr[5]);

IpAddr ethIp = eth.getIp(); printf("IP address : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);

NTPClient ntp; printf("NTP setTime...\n"); Host server(IpAddr(), 123, "pool.ntp.org"); printf("Result : %d\n", ntp.setTime(server));

time_t ctTime = time(NULL);

printf("\nTime is now (UTC): %d %s\n", ctTime, ctime(&ctTime));

Host host(IpAddr(), PORT, SERVER); SMTPClient smtp(host, DOMAIN, USER, PASSWORD, SMTP_AUTH_PLAIN);

EmailMessage msg; msg.setFrom(FROM_ADDRESS); msg.addTo(TO_ADDRESS);

msg.printf("Subject: mbed SMTPClient test at %s", ctime(&ctTime)); msg.printf("Shallow sensor is %s\n", bufshallow[vs]); msg.printf("Deep sensor is %s\n", bufdeep[vd]); msg.printf("Valve is %s\n", bufvalve[vv]);

printf("Last response | %s", smtp.getLastResponse().c_str()); printf("email sent");

goto stub;

/*Timer tm; tm.start();

while (true) { if (tm.read() > 0.5) { led1 = !led1; tm.start();*/ } } }

26 Mar 2011

/media/uploads/phase/datainprintftweetlcdset_textcopy.txt

Here is the program that makes a measurement at two set times (1900 and 0000), data received thru SPI, tweeted, emailed and then waits for next set time for measurement while showing current time. The initial program without email but with twitter worked well and tested for several weeks, measurements were made and tweeted on schedule. After adding email (after goto twit;) the program will make the first measurement, tweet and email successfully then hangs up after wait(10); lcd.cls(); (i.e. the lcd displays for 10 seconds and clears then the program hangs up) Any help would be appreciated. I removed passwords, etc from the program

03 Apr 2011

Hi Dan

Was there a reason why you moved the declaration EthernetNetIf eth(HOSTNAME); inside the main function (and especially inside the stub that is called repeatedly)?

There are issues with memory management on the mbed which may not help this approach.

Regards
Daniel

09 Apr 2011

Hi Dan

did you ever get your program sample "/media/uploads/phase/datainprintftweetlcdset_textcopy.txt" running perfect?

11 Apr 2011

Daniel

I took the declaration EthernetNetIf eth(HOSTNAME); out of the main function but still getting the same failure on my version of SMTPClient_HelloWorld

Voy ager

the program datainprintftweetlcdset is not running yet, I have not been able to fix it.

Thanks in advance for any help

Dan

12 May 2011

I'm trying to run this example, but I keep getting this message: "Response 41 501 | 501 Command "EHLO" requires an argument"

Does anyone know what can be done?

12 May 2011

Hi Marcos

Can you publish your program and post the link here? Take the passwords out of course!

Regards
Daniel

19 May 2011

Hi Daniel

is there any possibilty to close a Host or a smtpclient?

Host host(IpAddr(), PORT, SERVER); SMTPClient smtp(host, DOMAIN, USER, PASSWORD, SMTP_AUTH_PLAIN);

Such as host.close(); or smtp.close();

Regards

20 May 2011

Hi voy

There is a close method on smtp but it's protected (mainly because the SMTPClient code was based on the HTTPClient class which also has that method protected).

You could edit the class and make the method public rather than protected, and see if that does what you want.

Regards
Daniel

21 May 2011

Hi Daniel,

solved the problem.

Thanks for your help.

21 May 2011

@ Dan Koellen

Just declare these two lines in your program before int main() { and it works, at least in my system.

Code

            Host host(IpAddr(), PORT, SERVER);
            SMTPClient smtp(host, DOMAIN, USER, PASSWORD, SMTP_AUTH_PLAIN);


Code

//EthernetNetIf eth;
NTPClient ntp;
EthernetNetIf eth(HOSTNAME);
TextLCD lcd(p11, p12, p14, p15, p16, p17,TextLCD::LCD20x4); // rs, e, d4-d7
Host host(IpAddr(), PORT, SERVER);
SMTPClient smtp(host, DOMAIN, USER, PASSWORD, SMTP_AUTH_PLAIN);

int main() {
..........

Quote:

/media/uploads/phase/datainprintftweetlcdset_textcopy.txt

Here is the program that makes a measurement at two set times (1900 and 0000), data received thru SPI, tweeted, emailed and then waits for next set time for measurement while showing current time. The initial program without email but with twitter worked well and tested for several weeks, measurements were made and tweeted on schedule. After adding email (after goto twit;) the program will make the first measurement, tweet and email successfully then hangs up after wait(10); lcd.cls(); (i.e. the lcd displays for 10 seconds and clears then the program hangs up) Any help would be appreciated. I removed passwords, etc from the program

06 Jun 2011

@ voy ager I made the declaration before int main{}. the program now appears to work only by removing the declarations in main() and by emailing prior to tweeting. For some reason the email fails if it is done after tweeting as in the original order. I am now testing over a period of several days. Thank you for your help.

04 Jul 2011

user Dan Koellen wrote:

@ voy ager I made the declaration before int main{}. the program now appears to work only by removing the declarations in main() and by emailing prior to tweeting. For some reason the email fails if it is done after tweeting as in the original order. I am now testing over a period of several days. Thank you for your help.

Still working well!

05 Jul 2011

@ Dan Koellen

nice to hear. My system is also still alive and working.

05 Aug 2011

Hello all:

I think that I am a bit over my head here. I am attempting to email using this test program through a gmail account and I am not sure if I am setting things up correct or if our firewall or something similar is causing me grief.

All that I have changed is the following:

<<#define HOSTNAME "mbedSE"

server is domain name eg. mail.authsmtp.com port is smtp port eg. 25 or 23 domain must be acceptable to server (but often is not strictly enforced since does not affect FROM address)

  1. define SERVER "smtp.gmail.com"
  2. define PORT 25
  3. define USER "Steve"
  4. define PASSWORD "*"
  5. define DOMAIN "google.com"
  6. define FROM_ADDRESS "steve@gmail.com"
  7. define TO_ADDRESS "steve@destiny.com" >>

with the result of

<<

/* Segundo Equipo SMTPClient library demonstration */ Make sure you have modified the SERVER, PORT etc in the source code Setting up 1... Connected OK HW address : 00:02:f7:f0:65:f2 IP address : 172.24.124.164 NTP setTime... Result : 4

Time is now (UTC): 1312543259 Fri Aug 5 11:20:59 2011

[workspaces/0/users/960ffe63a8d7dcfbf2ddbd36223ee2d9/SMTPClient_HelloWorld/lib/NetServices/services/email/smtp/SMTPClient.cpp:SMTPClient@53] New SMTPClient 10007ea4 [workspaces/0/users/960ffe63a8d7dcfbf2ddbd36223ee2d9/SMTPClient_HelloWorld/lib/NetServices/services/email/smtp/SMTPClient.cpp:setup@175] DNSRequest 10001ab8 [workspaces/0/users/960ffe63a8d7dcfbf2ddbd36223ee2d9/SMTPClient_HelloWorld/lib/NetServices/services/email/smtp/SMTPClient.cpp:onDNSReply@233] Could not resolve hostname. Send result 2 Last response |

>>

Not sure where to go from here. Your help is appreciated.

05 Aug 2011

Hello Steve,

Although I am not up to speed on the MBED TCP/IP stack or the SMTP client, it would appear that the routine is unable to resolve the hostname "stmp.gmail.com" likely due to no available DNS server. Are there defines in the TCP/IP library to enter DNS server IP address(es)?

Hope this helps.

Doug

21 Nov 2011

"Must issue a STARTTLS command first. 5sm32276904eev.2<\r><\n>" i got this prob in the end some one help me?

23 Jan 2012

Hello,

I have a problem with the SMTPClientExample Program, I get the Mail in my E-Mail Client but FROM fails and the Date with Hour

Has any other one the same failure?

tia

11 Mar 2012

Hi everyone,

I got this example to work quite well with one exception: the recipient is recognized and accepted in the TCPSocket-Traffic but does not show up in the 'To:' field of my Email-Client (Outlook 2007). Instead, the plain text header shows 'Undisclosed recipients' in the rcpt field. As far as I could find out, this is the case when the recipient is left blank or is put into the bcc: address field. Looking at the code in EmailMessage, the problem must be somewhere around the <m_lTo.push_back(to)> line using a vector<string>. Could there be an issue with utf-8 characters ? Maybe a missing or misinterpreted <CRLF>? Also, I had to add a couple of "\r\n"s in my code before the email body showed up correctly (after the subject line and before text body).

Please login to post comments.