简体   繁体   中英

C - use libcurl to list imap sent mails

I use libcurl library to get all email in sent mail box from url : imaps://imap.gmail.com:993/[Gmail]/Sent Mail .But it not run and show error :

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

Please help me to check it . Thanks in advance. 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;
}

The problem is in your IMAPS URI. According to RFC3501 , mailbox names with spaces must be enclosed between double quotes " when used with SELECT or any other IMAP command.

The following should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM