首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cURL终端命令执行成功,而libcurl程序不执行

cURL终端命令执行成功,而libcurl程序不执行
EN

Stack Overflow用户
提问于 2017-04-17 10:23:44
回答 1查看 124关注 0票数 1
代码语言:javascript
复制
#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代码是由命令行使用以下命令生成的:

代码语言:javascript
复制
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

而命令

代码语言:javascript
复制
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无法发送邮件?之后,详细选项的输出将停止响应。

代码语言:javascript
复制
< 354 Go ahead [somecharacters].43 -gsmtp

mail.txt文件是:

代码语言:javascript
复制
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!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-17 11:53:37

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

代码语言:javascript
复制
curl_easy_setopt(hnd, CURLOPT_READDATA, fd);

整个代码看起来应该是:

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43449624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档