Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

Download the original Gmail message as EML

See more GMail REST API Examples

Supply a Gmail message ID and request format=raw. Decode the complete MIME message directly to bytes and save them in downloaded.eml. Parse those same bytes with Chilkat.Email to read the subject, sender, and attachment count without a text-encoding round trip.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
#include <CkEmailW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkJsonObjectW token;
    success = token.LoadFile(L"gmail-token.json");
    if (success != true) {
        wprintf(L"Could not read gmail-token.json from the private working directory.\n");
        return;
    }

    if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
        wprintf(L"An OAuth access token is required.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(token.stringOf(L"access_token"));
    CkHttpResponseW resp;
    CkJsonObjectW reply;
    CkStringBuilderW url;
    url.Append(L"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
    url.Append(L"GMAIL_MESSAGE_ID");
    url.Append(L"?format=raw");
    success = http.HttpNoBody(L"GET",url.getAsString(),resp);
    if (resp.get_StatusCode() == 0) {
        wprintf(L"Gmail did not return an HTTP response.\n");
        return;
    }

    if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        wprintf(L"Gmail HTTP %d\n",resp.get_StatusCode());
        success = reply.Load(resp.bodyStr());
        if (success == true) {
            wprintf(L"%s\n",reply.stringOf(L"error.message"));
        }

        return;
    }

    success = reply.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Expected a Gmail JSON response.\n");
        return;
    }

    if ((reply.JsonTypeOf(L"raw") != 1) || (reply.StringOfEquals(L"raw",L"",true) == true)) {
        wprintf(L"Expected raw in the response.\n");
        return;
    }

    CkBinDataW mime;
    success = reply.BytesOf(L"raw",L"base64url",mime);
    if (success != true) {
        wprintf(L"Could not decode the raw MIME.\n");
        return;
    }

    success = mime.WriteFile(L"downloaded.eml");
    if (success != true) {
        wprintf(L"Could not write downloaded.eml.\n");
        return;
    }

    CkEmailW email;
    success = email.SetFromMimeBd(mime);
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    wprintf(L"From: %s\n",email.ck_from());
    wprintf(L"Subject: %s\n",email.subject());
    wprintf(L"Attachments: %d\n",email.get_NumAttachments());
    }