Sending Email in C++: Library Overview and Practical Examples
Sending email is one of the standard tasks in server and desktop applications. Although C++ does not have built-in support for working with mail protocols, there are several mature libraries that allow you to integrate email sending (including attachments and TLS/SSL encryption) into your project. In this article, we will look at three main approaches: using libcurl, the VMime library, and POCO C++ Libraries.
1. Sending Email with libcurl
libcurl is a powerful cross-platform library for data transfer over various protocols, including SMTP, SMTPS, and SMTP with STARTTLS. It is part of the cURL project and is available for almost all operating systems.
Installing libcurl
On Ubuntu/Debian:
sudo apt-get install libcurl4-openssl-devOn macOS (via Homebrew):
brew install curlOn Windows, you can download pre-built DLLs or build it via vcpkg.
Example: Simple Email Without Attachments
The following code connects to an SMTP server (Gmail SMTP with SSL), authenticates, and sends a text email.
#include <iostream>#include <string>#include <curl/curl.h>
int main() { CURL *curl = curl_easy_init(); if (!curl) return 1;
// Gmail SMTP settings curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465"); curl_easy_setopt(curl, CURLOPT_USERNAME, "your_email@gmail.com"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "your_app_password");
// Email data const char *payload_text = "To: recipient@example.com\\r" "From: your_email@gmail.com\\r" "Subject: Test email from C++\\r" "\\r" "Hello! This email was sent using libcurl.\\r";
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); curl_easy_setopt(curl, CURLOPT_READDATA, payload_text);
// Send CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) std::cerr << "Error: " << curl_easy_strerror(res) << std::endl;
curl_slist_free_all(recipients); curl_easy_cleanup(curl); return 0;}libcurl Features
- Support for SMTP, SMTPS (SSL/TLS), and STARTTLS.
- Ability to send attachments via MIME formatting (manual or using
curl_mime). - Low-level control — you manually compose headers and the email body.
- Excellent documentation and community support.
2. VMime Library
VMime is a specialized C++ library for working with email. It provides a high-level API for creating, parsing, and sending messages with support for MIME, attachments, and all modern mail protocols.
Installing VMime
It is recommended to build from source using CMake:
git clone https://github.com/kisli/vmime.gitcd vmimemkdir build && cd buildcmake .. -DVMIME_BUILD_SAMPLES=ONmake -j4sudo make installFor SSL support, the libgsasl and libssl-dev libraries are required.
Example: Sending an Email with an Attachment
#include <vmime/vmime.hpp>#include <iostream>
int main() { try { // Create a session vmime::shared_ptr<vmime::net::session> session = vmime::net::session::create();
// Configure transport (SMTP with SSL) vmime::shared_ptr<vmime::net::transport> trans Also in library
Wire for Go: Complete Guide with Examples
Compression for JavaScript (Node.js): Complete Guide with Examples