I needed to send an e-mail through my C++ program and was able to do it using Libcurl. Since I had a hard time initially, I decided to share the following with you.
First download libcurl. I found binaries for my OS, Mac OSX, with some instructions on how to unpackage it making installation a breeze.
Compiling a Sample SMTP Program with Authentication and Transport Security
I found a sample smtp-tls program in which I simply saved and compiled to make sure libcurl was linking properly. If it isn’t, try compiling it in the following manner:
g++ smtp-tls.c -o smtp-tls -lcurl
Modifying the Sample to Work with GMail
I found the configuration instructions in the following support page: Configuring Other Mail Clients.
I modified the following lines in the smtp-tls program to make it work with GMail.
First, I changed the following FROM, TO, CC variables from
#define FROM "<firstname.lastname@example.org>" #define TO "<email@example.com>" #define CC "<firstname.lastname@example.org>"
to the following.
#define FROM "<email@example.com>" #define TO "<firstname.lastname@example.org>" #define CC "<email@example.com>"
Then I changed the following line setting the URL from
curl_easy_setopt(curl, CURLOPT_URL, "smtp://mainserver.example.net:587");
to the following.
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
I then commented out the following line
since I kept receiving errors about certificates and wasn’t sure how to fix this. (If this isn’t done properly, please let me know otherwise!)
Afterwards, I simply put my e-mail address and password
curl_easy_setopt(curl, CURLOPT_USERNAME, "firstname.lastname@example.org"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "PASSWORD123");
The console looked in the following manner:
* About to connect() to smtp.gmail.com port 587 (#0) * Trying 220.127.116.11... * connected * Connected to smtp.gmail.com (18.104.22.168) port 587 (#0) < 220 mx.google.com ESMTP q10sm8033678pbb.10 > EHLO unknownc42c0304eee8 < 250-mx.google.com at your service, [22.214.171.124] < 250-SIZE 35882577 < 250-8BITMIME < 250-STARTTLS < 250 ENHANCEDSTATUSCODES > STARTTLS < 220 2.0.0 Ready to start TLS * SSL connection using RC4-SHA * Server certificate: * subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=smtp.gmail.com * start date: 2011-11-18 01:57:17 GMT * expire date: 2012-11-18 02:07:17 GMT * common name: smtp.gmail.com (matched) * issuer: C=US; O=Google Inc; CN=Google Internet Authority * SSL certificate verify ok. > EHLO unknownc42c0304eee8 < 250-mx.google.com at your service, [126.96.36.199] < 250-SIZE 35882577 < 250-8BITMIME < 250-AUTH LOGIN PLAIN XOAUTH < 250 ENHANCEDSTATUSCODES > AUTH PLAIN a3VyaXlha2lAA21haWwuY291AGt1cml5YWtpQGdtYWlsLmNvbQBHTUF0b3MyNzEzdB== < 235 2.7.0 Accepted > MAIL FROM:<email@example.com> < 250 2.1.0 OK q10sm8033678pbb.10 > RCPT TO:<firstname.lastname@example.org> < 250 2.1.5 OK q10sm8033678pbb.10 > RCPT TO:<email@example.com> < 250 2.1.5 OK, duplicate recipients will be consolidated. q10sm8033678pbb.10 > DATA < 354 Go ahead q10sm8033678pbb.10 < 250 2.0.0 OK 1329280154 q10sm8033678pbb.10 * Connection #0 to host smtp.gmail.com left intact > QUIT < 221 2.0.0 closing connection q10sm8033678pbb.10 * Closing connection #0
I checked my e-mail, and there it was! If you wish to modify the contents of the e-mail, simply modify the payload.
Let me know if you encountered any problems.