Sending Email in C++: Overview of Libraries and Tools
Sending email is one of the basic tasks for many applications, from monitoring systems to user notifications. In C++, this task can be solved in several ways — from direct use of sockets to powerful libraries that support encryption, MIME, and attachments. In this article, we will look at the best tools for sending email in C++ and show code examples.
Why C++ for Sending Email?
C++ gives you full control over network connections, performance, and memory. However, the C++ standard library does not contain built-in tools for working with mail protocols (SMTP, POP3, IMAP). Therefore, developers either use third-party libraries or implement the protocol manually via sockets. We will focus on ready-made solutions that save time and ensure stability.
Main Libraries for Sending Email in C++
- libcurl — the most popular cross-platform library for data transfer. Supports SMTP, SMTP with SSL/TLS, authentication, and MIME attachments.
- VMime — a specialized library for working with email. Full support for MIME, S/MIME encryption, custom headers, and all mail protocols.
- c-icap and libESMTP — lower-level libraries focused on SMTP sessions.
- Boost.Asio + custom SMTP implementation — for those who want minimal dependencies and are willing to write the protocol manually.
In this article, we will focus in detail on libcurl, as it is the most versatile and widely used.
Installing libcurl
To get started with libcurl, you need to install it:
- Linux (Ubuntu/Debian):
sudo apt install libcurl4-openssl-dev - macOS:
brew install curl - Windows: download pre-built binaries from the official website or use vcpkg:
vcpkg install curl
Example 1: Sending a Simple Email via Gmail (SSL)
This example uses libcurl to send an email through the Gmail SMTP server with SSL encryption.
#include <stdio.h>#include <string.h>#include <curl/curl.h>
int main(void) { CURL *curl; CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init();
if(curl) { // SMTP settings curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465"); curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); curl_easy_setopt(curl, CURLOPT_USERNAME, "your.email@gmail.com"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "app_password");
// Sender and recipient curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "<your.email@gmail.com>"); struct curl_slist *recipients = NULL; recipients = curl_slist_append(recipients, "<recipient@example.com>"); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
// Email body const char *payload_text = "From: Your Name <your.email@gmail.com>\\r" "To: Recipient <recipient@example.com>\\r" "Subject: Test email from C++\\r" "\\r" "Hello! This email was sent using libcurl in C++.\\r";
curl_easy_setopt(curl, CURLOPT_READDATA, payload_text); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
// Execute res = curl_easy_perform(curl); if(res != CURLE_OK) fprintf(stderr, "Error: %s", curl_easy_strerror(res)); else printf("Email sent!");
curl_slist_free_all(recipients); curl_eas