C - 使用libcurl列出imap发送的邮件

时间:2013-12-19 07:25:58

标签: c imap

我使用libcurl库从url:imaps://imap.gmail.com:993 / [Gmail] /已发邮件中获取已发送邮箱中的所有电子邮件。但它没有运行并显示错误:

curl_easy_perform() failed: URL using bad/illegal format or missing URL

请帮我检查一下。提前致谢。 Toan Nguyen

struct string {
 char *ptr;
 size_t len;
};

void init_string(struct string *s) {
  s->len = 0;
  s->ptr = malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
    exit(EXIT_FAILURE);
 }
 s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
 size_t new_len = s->len + size*nmemb;
 s->ptr = realloc(s->ptr, new_len+1);
 if (s->ptr == NULL) {
   fprintf(stderr, "realloc() failed\n");
   exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
 s->ptr[new_len] = '\0';
 s->len = new_len;

 return size*nmemb;
}

int main(int argc,char *argv[])
{
 CURL *curl;
 CURLcode res;  

 curl = curl_easy_init();
if(curl) {
   struct string s;
   init_string(&s); 
curl_easy_setopt(curl,CURLOPT_USERNAME,argv[1]);
  curl_easy_setopt(curl,CURLOPT_PASSWORD,argv[2]);
curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993/%5BGmail%5D%2FSent%20Mail;UID=*");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);

printf("%s\n", s.ptr);
free(s.ptr);

/* Check for errors */ 
 if(res != CURLE_OK)
 fprintf(stderr, "curl_easy_perform() failed: %s\n",
        curl_easy_strerror(res));

/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

1 个答案:

答案 0 :(得分:0)

问题出在您的IMAPS URI中。根据{{​​3}},当与SELECT或任何其他IMAP命令一起使用时,带有空格的邮箱名称必须用双引号"括起来。

以下内容应该有效:

curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993/\"[Gmail]/All Mail\"");