Search code examples
c++cemailimaplibcurl

libcurl IMAP not working


Using the below code, I'm trying to get any of the libcurl IMAP commands to work.

Currently, regardless of the command set via CURLOPT_CUSTOMREQUEST, in my callback function the only data that is given is the oldest email (1st) in my inbox. I can even put something like "dfsafdasfasfaf" in the CURLOPT_CUSTOMREQUEST, and no error will be shown, and the oldest email will be printed from the callback.

I've tried using the sample codes on libcurl's site, to list folders, LSUB, etc and it's always the same - the only thing returned is the contents of the 1st email in my inbox.

I'm using curl 7.40 mingw32 on win32 g++ (-lcurldll).

Surely I must be doing something wrong. If you could take a moment to correct my error, I would be most appreciative. Thank you.

EDIT - Even if you don't know the answer, could you please leave a comment if you have successfully gotten libcurl IMAP to work before? Because if no one has gotten libcurl imap to work before I'll stop wasting my time with it and move on to VMime or another option..

EDIT2- My principal question is how can I list folders via libcurl?

size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{   
    printf("%s\n", buf);

    return size*nmemb; //tell curl how many bytes we handled
} 


int main(void)
{
    CURL *curl;
    CURLcode res = CURLE_OK;

    curl = curl_easy_init();
    if(curl) {

        curl_easy_setopt(curl, CURLOPT_USERNAME, "gmailuser");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");


        curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com/INBOX");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "LIST");

        res = curl_easy_perform(curl);


        if(res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",  curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);
   }

  _getch (); 
  return (int)res;

}

Solution

  • In order to get list of folders in a given GMail inbox, you should use:

    curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com/");
    

    Also, I believe you don't need this line to perform LIST request:

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "LIST");
    

    I have tested it on Linux, on libcurl version 7.35.0, though I believe the problems you are encountering are not OS-specific and are rather caused by the current state of the implementation of IMAP support in the library. You can find source code for libcurl version 7.35.0 here.

    You can also find more examples of current libcurl IMAP support on the examples page (see the links on the right for more detailed examples).