Reading emails from gmail POP3 account using libCurl

后端 未结 1 1970
半阙折子戏
半阙折子戏 2021-02-02 03:18

I\'m currently working in a C++ project which has to be able to read emails from an gmail POP3 account just like the title says. Also is important to say that I need to download

相关标签:
1条回答
  • 2021-02-02 04:15

    I made it guys! Here's a good sample about how you can access a gmail acount and see what's inside. I'm currently working on parsing the information inside, because actually it only retrieves the amount of mails on inbox and its size. If you change the url as "pops://pop.gmail.com:995/1" it will return the content of the messege and also including a base64 encoding for the attachments. Thanks Anyway... here is the code! :)

    #pragma region Types
    struct MemoryStruct {
        char *memory;
        size_t size;
    };  
    #pragma endregion
    
    void MailServer::Open(char *username,char *password)
    {
        m_username = username;
        m_password = password;
        struct MemoryStruct chunk;
    
        chunk.memory = (char*) malloc(1);  //crecerá según sea necesario con el realloc
        chunk.size = 0;    //no hay datos en este punto
    
        //inicializacion
        curl_global_init(CURL_GLOBAL_ALL);
        curl = curl_easy_init();
    
    
        //login
        curl_easy_setopt(curl,CURLOPT_USERNAME,username);
        curl_easy_setopt(curl,CURLOPT_PASSWORD,password);
    
        m_popsAccount = "pop3s://pop.gmail.com:995/";       
    
        curl_easy_setopt(curl, CURLOPT_URL, m_popsAccount.c_str());
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); 
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); 
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); 
    
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); 
    
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
    
        //some servers needs this validation
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
    
        res = curl_easy_perform(curl); 
    
        if(res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));
        }
        else 
        {
            /*
            here is where you can work with the data inside the chunk...
            */  
            printf("%s\n",chunk.memory); //here is the information
            printf("%lu bytes retrieved\n", (long)chunk.size);  
      }
    
        //se libera la memoria si hay datos
        if(chunk.memory)
            free(chunk.memory);
        /* always cleanup */ 
    
        curl_global_cleanup();
    }
    
    
    size_t MailServer::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
    {
        size_t realsize = size * nmemb;
        struct MemoryStruct *mem = (struct MemoryStruct *)userp;
    
        mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
        if(mem->memory == NULL) {
            /* out of memory! */ 
            printf("not enough memory (realloc returned NULL)\n");
            return 0;
        }
    
        memcpy(&(mem->memory[mem->size]), contents, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
    
        return realsize;
    }
    
    0 讨论(0)
提交回复
热议问题