Libcurl不会发送SMTP退出命令

时间:2014-10-21 03:17:59

标签: c++ c curl smtp libcurl

#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>

#define server "smtps://smtp.gmail.com"
#define to "email@gmail.com"
#define from "email@gmail.com"
#define pwd "****"

typedef struct
{
    char** mem;
    size_t size;
} payload_data;

size_t reader(void *ptr, size_t size, size_t nmemb, void *userp)
{
    char* str = NULL;
    payload_data *data = (payload_data *)userp;

    if((size == 0) || (nmemb == 0) || ((size * nmemb) < 1))
        return 0;

    str = data->mem[data->size];

    if (str)
    {
        size_t len = strlen(str);
        memcpy(ptr, str, len);
        ++data->size;
        return len;
    }

    return 0;
}

void TestCurl()
{
    static const char *info[] =
    {
        "To: <"to">\r\n"
        "From: <"from">\r\n",
        "Subject: TestCurl\r\n",
        "\r\n",
        "Messaging with libcurl..\r\n",
        "\r\n",//"\r\n.\r\nQUIT\r\n",
        NULL
    };

    CURLcode res = CURLE_OK;
    payload_data data = {0};
    CURL* curl = curl_easy_init();
    struct curl_slist* recipients = NULL;

    if (curl)
    {
        data.mem = info;
        curl_easy_setopt(curl, CURLOPT_URL, server);
        curl_easy_setopt(curl, CURLOPT_USERNAME, from);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, pwd);
        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
        recipients = curl_slist_append(recipients, to);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, reader);
        curl_easy_setopt(curl, CURLOPT_READDATA, &data);
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); //DOES NOT WORK WITHOUT THIS!
        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
          fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        curl_slist_free_all(recipients);
        curl_easy_cleanup(curl);
    }
}

结果是:

* Rebuilt URL to: smtps://smtp.gmail.com/
* timeout on name lookup is not supported
* Hostname was NOT found in DNS cache
*   Trying 74.125.69.108...
* Connected to smtp.gmail.com (74.125.69.108) port 465 (#0)
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
*        subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=smtp.gm
ail.com
*        start date: 2014-07-15 08:40:38 GMT
*        expire date: 2015-04-04 15:15:55 GMT
*        subjectAltName: smtp.gmail.com matched
*        issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*        SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
< 220 mx.google.com ESMTP 141sm5490110ioz.39 - gsmtp
> EHLO Kira
< 250-mx.google.com at your service, [my_ip_address]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-CHUNKING
< 250 SMTPUTF8
> AUTH LOGIN
< 334 VXNlcm5hbWU6
> encrypted_username
< 334 UGFzc3dvcmQ6
> encrypted_password
< 235 2.7.0 Accepted
> MAIL FROM:<email@gmail.com>
< 250 2.1.0 OK 141sm5490110ioz.39 - gsmtp
> RCPT TO:<email@gmail.com>
< 250 2.1.5 OK 141sm5490110ioz.39 - gsmtp
> DATA
< 354  Go ahead 141sm5490110ioz.39 - gsmtp
< 250 2.0.0 OK 1413861121 141sm5490110ioz.39 - gsmtp
* Connection #0 to host smtp.gmail.com left intact

Process returned 0 (0x0)   execution time : 1.524 s
Press any key to continue.

我无法发送QUIT。我尝试将QUIT添加到&#34;信息&#34;数组,但它只是将其添加为消息的一部分。我尝试用\r\n\r\n.\r\n终止但没有雪茄..

电子邮件已成功发送..我无法让它退出。每当我发送一封新电子邮件时,它都会增加&#34;连接#X以主机smtp.gmail.com保持原样&#34; ..即使我做了卷曲清理..

我有什么想法可以让它退出并停止主机完好无损?它在某处泄漏了吗?为什么这样做?

1 个答案:

答案 0 :(得分:1)

首先,当你关闭easy handle时它实际上会关闭连接 - 因为这是使用easy界面时保持连接缓存的地方。根据您的libcurl版本,它可能不会发送QUIT,您可能无法在详细输出中看到它(因为在您将CURLOPT_VERBOSE设置为TRUE的句柄已经消失之后完成关闭)。

在特定传输完成后强制关闭连接的libcurl方法是设置CURLOPT_FORBID_REUSE。以类似的精神,您可以设置CURLOPT_FRESH_CONNECT以强制使用新连接,而不是重新使用旧连接。