You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于libcurl的C++ SMTP邮件发送GitHub库超时故障求助

Troubleshooting libcurl SMTP Timeout with Gmail & Adding PDF Attachments

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 CurlSmtp class is properly setting up curl to use SSL for this port. Sometimes libcurl needs explicit flags like CURLOPT_USE_SSL set to CURLUSESSL_ALL to 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 465 or nc -zv smtp.gmail.com 465—if these fail, you’ll need to adjust firewall rules.

Fixing the Timeout: First Actions

  1. Enable Verbose Logging: Add this line to the CurlSmtp initialization 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.

  2. 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).

  3. Check libcurl SSL Support: Run curl --version in 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 if send_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/delete with std::unique_ptr<CurlSmtp> to avoid memory leaks in your larger codebase.
  • Error Handling: Expand error checking beyond just the timeout—libcurl and the CurlSmtp class likely return specific error codes you can use to debug further.

内容的提问来源于stack exchange,提问作者PinkFloyd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:15:12