I needed to send emails in a product written in C++, so I searched the Internet and found a great article: SMTP Client written by Jakub Piwowarczyk. However, many of my clients use SMTP servers that require secure connection (TLS or SSL), and the SMTP Client does not support it. So I had to add SSL/TLS support to the CSmtp class from SMTP Client before I could use it in my product. As I was new to SSL/OpenSSL, it did take me quite some time to learn how to use it properly, and to make the code work with several popular SMTP servers. I have also seen people searching the internet looking for a C++ implementation of SMTP/SSL/TLS, but just could not find one. So I decided to share the one I wrote, in the hope that it will save people who are not familiar with SSL some time.
CSmtp
Note that this article does not cover the details of SMTP. Please go to the original article SMTP Client if you need to know more about SMTP.
There are 2 kinds of secure connections for SMTP, one is SSL and the other is TLS. Some SMTP servers support only one kind and some support both. Generally speaking, the port for SSL is 465, and the port for TLS is 587, but this is not always the case. In addition to the ports being different, SMTP/SSL is different than SMTP/TLS in that, SMTP/SSL negotiates an encrypted connection directly after the underlying TCP connection has been established, while SMTP/TLS requires that the client send a STARTLS command to the server before they negotiate an encrypted connection.
STARTLS
The steps involved in SMTP/SSL are as follows:
The steps involved in SMTP/TLS are as follows:
STARTTLS
I have used openssl (http://www.openssl.org) in the sample code. The directory "openssl-0.9.8l" in the sample code contains all the necessary header files and the two pre-built static openssl libraries. If you would also like to use this version of openssl in your code, be sure to copy the entire "openssl-0.9.8l" directory to the root directory of your project and add "openssl-0.9.8l\inc32" to "Additional Include Directories", and also add "openssl-0.9.8l\out32" to "Additional Library Directories".
static openssl
openssl
If you would like to build your own openssl, please refer to http://www.openssl.org for detailed instructions.
#define test_gmail_tls CSmtp mail; #if defined(test_gmail_tls) mail.SetSMTPServer("smtp.gmail.com",587); mail.SetSecurityType(USE_TLS); #elif defined(test_gmail_ssl) mail.SetSMTPServer("smtp.gmail.com",465); mail.SetSecurityType(USE_SSL); #elif defined(test_hotmail_TLS) mail.SetSMTPServer("smtp.live.com",25); mail.SetSecurityType(USE_TLS); #elif defined(test_aol_tls) mail.SetSMTPServer("smtp.aol.com",587); mail.SetSecurityType(USE_TLS); #elif defined(test_yahoo_ssl) mail.SetSMTPServer("plus.smtp.mail.yahoo.com",465); mail.SetSecurityType(USE_SSL); #endif mail.SetLogin("***"); mail.SetPassword("***"); mail.SetSenderName("User"); // ...... mail.Send();
If you use a non-privileged user account to test Yahoo, the mail will fail to send. And the error message returned by the Yahoo SMTP server is "530 Access denied: Free users cannot access this server".
Thanks to everyone for the effective crowdsourcing! Please keep up the improvements to our library! Added fix contributed GKarRacer for the improper checking of the MsgBody.size() before working with a line of the message body. Moved memory allocation and checking if all attachments can be opened to before the MAIL command is issued so that if there is an issue with one of them, sending the email can be gracefully aborted without the email being sent without the attachments. Changed all the sprintf commands to snprintf to add greater security. #define'd snprintf to sprintf_s for MS Visual C. Also changed most strcpy calls to snprintf since the MS Visual C version of that function strcpy_s has the arguments re-ordered so it wouldn't be possible to use it without affecting portability. Added fix contributed by jcyangzh about a possible infinite loop in the SayQuit function. Added fixes contributed by sbrytskyy required to make the AUTH PLAIN login work properly.
Thanks to everyone for the effective crowdsourcing! Please keep up the improvements to our library!
MsgBody.size()
MAIL
sprintf
snprintf
#define
sprintf_s
strcpy
strcpy_s
SayQuit
AUTH PLAIN
SMTP_SECURITY_TYPE
SetCharSet()
SetReadReceipt()
std::string
SetLocalHostName()
m_sNameFrom
m_bAuthenticate
true
m_type
m_Authenticate
ConnectRemoteServer
SetSMTPServer
DisconnectRemoteServer()
Send()
ConnectRemoveServer()
ConnectRemoteServer()