cURL 终端命令成功执行,而 libcurl 程序没有

标签 c curl gmail libcurl smtps

#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;
  struct curl_slist *slist1;

  slist1 = NULL;
  slist1 = curl_slist_append(slist1, "reciever@gmail.com");

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, (curl_off_t)179);
  curl_easy_setopt(hnd, CURLOPT_URL, "smtps://smtp.gmail.com:465/mail.txt");
  curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);
  curl_easy_setopt(hnd, CURLOPT_USERPWD, "sender@gmail.com:senderPass");
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.47.0");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_setopt(hnd, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
  curl_easy_setopt(hnd, CURLOPT_MAIL_FROM, "sender@gmail.com");
  curl_easy_setopt(hnd, CURLOPT_MAIL_RCPT, slist1);
  curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);

  ret = curl_easy_perform(hnd);

  curl_easy_cleanup(hnd);
  hnd = NULL;
  curl_slist_free_all(slist1);
  slist1 = NULL;

  return (int)ret;
}

上面的 libcurl 代码是通过命令行使用以下命令生成的:

curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd --mail-from 'sender@gmail.com' --mail-rcpt 'receiver@gmail.com' --upload-file mail.txt --user 'sender@gmail.com:senderPass' --insecure --libcurl -myCode.c

虽然命令

curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd --mail-from 'sender@gmail.com' --mail-rcpt 'receiver@gmail.com' --upload-file mail.txt --user 'sender@gmail.com:senderPass' --insecure

邮件发送成功,为什么myCode.c发送邮件失败? 详细选项的输出在

之后停止响应
< 354 Go ahead [somecharacters].43 -gsmtp

mail.txt 文件是:

From: "I am Sender" <sender@gmail.com>
To: "I am Receiver" <receiver@gmail.com>
Subject: This is a test

Hi,
I’m sending this mail with curl through my gmail account.
Bye!

最佳答案

您必须使用 CURLOPT_READDATA 指定要读取的文件的文件描述符:

curl_easy_setopt(hnd, CURLOPT_READDATA, fd);

整个代码看起来像:

#include <curl/curl.h>

int main(int argc, char *argv[])
{
    CURLcode ret;
    CURL *hnd;
    struct curl_slist *slist1;

    slist1 = NULL;
    slist1 = curl_slist_append(slist1, "reciever@gmail.com");

    FILE *fd;

    fd = fopen("mail.txt", "rb");

    if(!fd)
        return 1; 

    hnd = curl_easy_init();
    curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, (curl_off_t)179);
    curl_easy_setopt(hnd, CURLOPT_URL, "smtps://smtp.gmail.com:465/mail.txt");
    curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L);

    curl_easy_setopt(hnd, CURLOPT_READDATA, fd); 

    curl_easy_setopt(hnd, CURLOPT_USERPWD, "sender@gmail.com:senderPass");
    curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.47.0");
    curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
    curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
    curl_easy_setopt(hnd, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
    curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
    curl_easy_setopt(hnd, CURLOPT_MAIL_FROM, "sender@gmail.com");
    curl_easy_setopt(hnd, CURLOPT_MAIL_RCPT, slist1);
    curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);

    ret = curl_easy_perform(hnd);

    curl_easy_cleanup(hnd);
    hnd = NULL;
    curl_slist_free_all(slist1);
    slist1 = NULL;

    return (int)ret;
}

关于cURL 终端命令成功执行,而 libcurl 程序没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43449624/

相关文章:

c++ - closesocket 线程安全吗?

c# - 如何在我的程序中实现 curl (Pushbullet API)?

php - 使用curl和PHP从mca.gov.in获取数据

php - 如何使用 Laravel http post 禁用 SSL 检查

html - 摆脱 Gmail 中的 "Show Quoted Text"

python - 如何使用 Python 通过 IMAP 访问 GMail 上的所有电子邮件?

c - 为什么参数类型不正确(应该是 int * 的时候是 int)?

代码不断崩溃(第 4.2 周已停止工作

c - 适当的内存分配?

html - GMail 中 <td> 的宽度无效