基于libcurl的C++ SMTP邮件发送GitHub库超时故障求助
Hey there! Let’s break down your problem and work through solutions step by step. I’ve dealt with similar libcurl SMTP headaches before, so let’s start with that frustrating timeout error first.
Why You’re Seeing That Timeout Message
The * Expire in 200 ms for 1 (transfer 0x558a3b95d7e0) line is libcurl’s way of saying it’s waiting for a response from Gmail’s SMTP server but isn’t getting one. For Gmail’s service, the most common culprits are:
- Gmail Security Lockdown: If you have 2-Step Verification enabled on your Gmail account, your regular password won’t work here. You need to generate an App Password specifically for this application (found in your Google Account settings under "Security"). If 2-Step is off, you might need to enable "Less secure app access" (note: Google is phasing this out, so App Passwords are the better long-term fix).
- Misconfigured SSL/TLS: Port 465 uses implicit SSL, so double-check that the
CurlSmtpclass is properly setting up curl to use SSL for this port. Sometimes libcurl needs explicit flags likeCURLOPT_USE_SSLset toCURLUSESSL_ALLto enforce secure connections. - Network Blockages: Your machine or server might be blocking outgoing traffic to port 465. Test connectivity first with
telnet smtp.gmail.com 465ornc -zv smtp.gmail.com 465—if these fail, you’ll need to adjust firewall rules.
Fixing the Timeout: First Actions
Enable Verbose Logging: Add this line to the
CurlSmtpinitialization code to get detailed curl debug output:curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);This will show you exactly where the connection is stalling—whether it’s a DNS issue, SSL handshake failure, or authentication problem.
Validate Credentials: Swap your regular password for an App Password if 2-Step is enabled. If not, confirm "Less secure app access" is turned on (again, App Passwords are safer long-term).
Check libcurl SSL Support: Run
curl --versionin your terminal—make sure "OpenSSL" (or another SSL backend) is listed. If not, you’ll need to recompile libcurl with SSL enabled.
Adding PDF Attachments
Since the honeyligo repo has well-commented code, it likely supports attachments via the constructor or a dedicated method. Here’s how to adjust your code to include a PDF:
#include <unistd.h> #include "curlsmtp.h" int main(){ // List your PDF paths here (supports multiple attachments) std::vector<std::string> attachments = {"/home/you/documents/student_report.pdf"}; CurlSmtp* mail = new CurlSmtp( "MYADDRESS@gmail.com", "YOUR_GMAIL_APP_PASSWORD", {"foo@yahoo.fr"}, {}, // Leave empty if no CC recipients {}, // Leave empty if no BCC recipients attachments, // Attachments passed here "Your Student Report", "Hi there! Attached is your requested report.", "smtp.gmail.com", "465" ); // Ditch the sleep(3) — check if send_mail() blocks until completion bool sendSuccess = mail->send_mail(); if(sendSuccess){ std::cout << "Mail sent successfully!" << std::endl; } else { std::cout << "Mail failed to send — check verbose logs for details." << std::endl; } delete mail; return 0; }
If the constructor doesn’t accept an attachments list, look for a method like mail->addAttachment("/path/to/file.pdf"); in the curlsmtp.h or .cpp files—the comments should point you to it.
Tips for Your Large Project
- Avoid
sleep(): Hardcoding a 3-second wait is unreliable. Confirm ifsend_mail()is synchronous (blocks until the mail is sent) or if you need to use a callback to track completion. - Smart Pointers: Replace raw
new/deletewithstd::unique_ptr<CurlSmtp>to avoid memory leaks in your larger codebase. - Error Handling: Expand error checking beyond just the timeout—libcurl and the
CurlSmtpclass likely return specific error codes you can use to debug further.
内容的提问来源于stack exchange,提问作者PinkFloyd

