Linux/Mac环境下使用C++实现邮件发送求助(原Matlab用户)
Hey there! I get it—switching from MATLAB to C++ can feel a bit daunting, especially when you need to replicate a handy feature like automated email alerts for long simulations. Let's fix this with libcurl, a reliable cross-platform library that makes sending emails over SMTP straightforward.
Step 1: Install libcurl
First, you'll need to have libcurl installed on your system—it's not included by default, but it's easy to get:
- For Linux (Ubuntu/Debian):
sudo apt-get update && sudo apt-get install libcurl4-openssl-dev - For Mac:
If you use Homebrew, run:brew install curl
Step 2: Example Code
Here's a complete, commented example that mirrors your MATLAB workflow—you just need to plug in your email details, SMTP server info, and message content:
#include <iostream> #include <curl/curl.h> #include <string> // Optional debug callback to troubleshoot connection issues static int debug_callback(CURL* curl, curl_infotype type, char* data, size_t size, void* userptr) { (void)curl; (void)userptr; if(type == CURLINFO_TEXT) { std::cout << "libcurl debug: " << data << std::endl; } return 0; } int main() { // Initialize libcurl CURL* curl = curl_easy_init(); if(!curl) { std::cerr << "Failed to initialize libcurl!" << std::endl; return 1; } // -------------------------- Configure Your Email Details -------------------------- const char* smtp_server = "smtp.gmail.com:587"; // Adjust for your email provider const char* from_email = "your-email@gmail.com"; const char* to_email = "recipient@example.com"; const char* email_subject = "Simulation Complete Alert"; const char* email_body = "Your long-running simulation has finished successfully!"; // For Gmail, use an App Password if 2FA is enabled; regular password otherwise const char* email_password = "your-app-password-or-regular-password"; // --------------------------------------------------------------------------------- // Build full email content (follows SMTP format) std::string email_data = "To: " + std::string(to_email) + "\r\n" "From: " + std::string(from_email) + "\r\n" "Subject: " + std::string(email_subject) + "\r\n\r\n" + std::string(email_body); // Set core libcurl options curl_easy_setopt(curl, CURLOPT_URL, smtp_server); curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); // Enforce SSL/TLS encryption curl_easy_setopt(curl, CURLOPT_USERNAME, from_email); curl_easy_setopt(curl, CURLOPT_PASSWORD, email_password); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from_email); // Add recipient to the email struct curl_slist* recipients = NULL; recipients = curl_slist_append(recipients, to_email); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); // Tell libcurl to use our email content as the upload data curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_READDATA, email_data.c_str()); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); // Uncomment below lines if you need to debug connection/authentication issues // curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_callback); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // Send the email CURLcode res = curl_easy_perform(curl); if(res != CURLE_OK) { std::cerr << "Failed to send email: " << curl_easy_strerror(res) << std::endl; } else { std::cout << "Email sent successfully!" << std::endl; } // Clean up resources curl_slist_free_all(recipients); curl_easy_cleanup(curl); return 0; }
Step 3: Compile the Code
When compiling with g++, you need to link against the libcurl library using the -lcurl flag. Run this command in your terminal:
g++ -std=c++11 email_sender.cpp -o email_sender -lcurl
Then execute the binary:
./email_sender
Key Tips & Troubleshooting
- SMTP Provider Variations: Different services use different servers/ports:
- Outlook/Hotmail:
smtp.office365.com:587 - Yahoo:
smtp.mail.yahoo.com:587
- Outlook/Hotmail:
- Security: Never hardcode passwords in production code! Use environment variables or a secure config file instead.
- Authentication Issues: For Gmail, if you have 2FA enabled, you must use an App Password (not your regular account password).
内容的提问来源于stack exchange,提问作者Pan

