SMTP Client with SSL/TLS

7390 단어 종합 하 다
본문 소스 코드 주소:http://download.csdn.net/detail/ilovethissite/5026181
Introduction
I needed to send emails in a product written in C++, so I searched the Internet and found a great article: SMTP Clientwritten 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 Clientbefore 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.
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.
Background
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.
The steps involved in SMTP/SSL are as follows:
The client connects to the server using TCP.
The client negotiates an encrypted connection with the server.
The server sends a welcome message using the encrypted connection to the client.
The client sends a EHLO command using the encrypted connection to the server.
The server responds to the EHLO command using the encrypted connection.
The steps involved in SMTP/TLS are as follows:
The client connects to the server using TCP.
The server sends a welcome message using the un-encrypted connection to the client.
The client sends a EHLO command using the un-encrypted connection to the server.
The server responds to the EHLO command using the un-encrypted connection.
The client sends a  STARTTLS  command using the un-encrypted connection to the server.
The server responds to the  STARTTLS  command using the un-encrypted connection.
The client negotiates an encrypted connection with the server.
The client sends a EHLO command using the encrypted connection to the server.
The server responds to the EHLO command using the encrypted connection.
Using the Code
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".
If you would like to build your own  openssl , please refer to http://www.openssl.org for detailed instructions.

 Collapse
 |  Copy Code
#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".
Notes
The code does not verify the server's identity, that is, it does not check the server's certificate. This is usually not a big problem if we make sure we feed the program with correct server addresses. However, it is still worth mentioning that there is the chance that we are talking to an impersonator if we don't check the certificate.
You are not allowed to use the code in this article for spamming.

좋은 웹페이지 즐겨찾기