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

Inspect the MIME parts of a Gmail message

See more GMail REST API Examples

Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.

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 C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkStringTable.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject token;
    HCkHttp http;
    HCkHttpResponse resp;
    HCkJsonObject reply;
    HCkStringBuilder url;
    HCkStringTable paths;
    HCkJsonObject part;
    HCkStringBuilder childPath;
    int p;
    int j;

    success = FALSE;

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

    token = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(token,"gmail-token.json");
    if (success != TRUE) {
        printf("Could not read gmail-token.json from the private working directory.\n");
        CkJsonObject_Dispose(token);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(token,"access_token") != 1) || (CkJsonObject_StringOfEquals(token,"access_token","",TRUE) == TRUE)) {
        printf("An OAuth access token is required.\n");
        CkJsonObject_Dispose(token);
        return;
    }

    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    CkHttp_putAuthToken(http,CkJsonObject_stringOf(token,"access_token"));
    resp = CkHttpResponse_Create();
    reply = CkJsonObject_Create();
    url = CkStringBuilder_Create();
    CkStringBuilder_Append(url,"https://gmail.googleapis.com/gmail/v1/users/me/messages/");
    CkStringBuilder_Append(url,"GMAIL_MESSAGE_ID");
    CkStringBuilder_Append(url,"?format=full");
    success = CkHttp_HttpNoBody(http,"GET",CkStringBuilder_getAsString(url),resp);
    if (CkHttpResponse_getStatusCode(resp) == 0) {
        printf("Gmail did not return an HTTP response.\n");
        CkJsonObject_Dispose(token);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(url);
        return;
    }

    if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) < 200) || (CkHttpResponse_getStatusCode(resp) >= 300)) {
        printf("Gmail HTTP %d\n",CkHttpResponse_getStatusCode(resp));
        success = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
        if (success == TRUE) {
            printf("%s\n",CkJsonObject_stringOf(reply,"error.message"));
        }

        CkJsonObject_Dispose(token);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(url);
        return;
    }

    success = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
    if (success != TRUE) {
        printf("Expected a Gmail JSON response.\n");
        CkJsonObject_Dispose(token);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(url);
        return;
    }

    if (CkJsonObject_JsonTypeOf(reply,"payload") != 3) {
        printf("Expected a MIME payload.\n");
        CkJsonObject_Dispose(token);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(url);
        return;
    }

    //  Queue paths into the original JSON document so any MIME nesting depth is supported.
    paths = CkStringTable_Create();
    success = CkStringTable_Append(paths,"payload");
    if (success != TRUE) {
        printf("Could not queue the root part.\n");
        CkJsonObject_Dispose(token);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        CkJsonObject_Dispose(reply);
        CkStringBuilder_Dispose(url);
        CkStringTable_Dispose(paths);
        return;
    }

    part = CkJsonObject_Create();
    childPath = CkStringBuilder_Create();
    p = 0;
    while ((p < CkStringTable_getCount(paths))) {
        success = CkJsonObject_ObjectOf2(reply,CkStringTable_stringAt(paths,p),part);
        if (success != TRUE) {
            printf("Could not read a MIME part.\n");
            CkJsonObject_Dispose(token);
            CkHttp_Dispose(http);
            CkHttpResponse_Dispose(resp);
            CkJsonObject_Dispose(reply);
            CkStringBuilder_Dispose(url);
            CkStringTable_Dispose(paths);
            CkJsonObject_Dispose(part);
            CkStringBuilder_Dispose(childPath);
            return;
        }

        printf("Part: %s  bytes: %d\n",CkJsonObject_stringOf(part,"mimeType"),CkJsonObject_IntOf(part,"body.size"));
        if (CkJsonObject_JsonTypeOf(part,"filename") == 1) {
            if (CkJsonObject_StringOfEquals(part,"filename","",TRUE) != TRUE) {
                printf("Filename: %s\n",CkJsonObject_stringOf(part,"filename"));
            }

        }

        if (CkJsonObject_JsonTypeOf(part,"body.attachmentId") == 1) {
            printf("Attachment ID: %s\n",CkJsonObject_stringOf(part,"body.attachmentId"));
        }
        else {
            if (CkJsonObject_JsonTypeOf(part,"body.data") == 1) {
                printf("Body data is available in this part.\n");
            }

        }

        j = 0;
        while ((j < CkJsonObject_SizeOfArray(part,"parts"))) {
            CkStringBuilder_SetString(childPath,CkStringTable_stringAt(paths,p));
            CkStringBuilder_Append(childPath,".parts[");
            CkStringBuilder_AppendInt(childPath,j);
            CkStringBuilder_Append(childPath,"]");
            success = CkStringTable_Append(paths,CkStringBuilder_getAsString(childPath));
            if (success != TRUE) {
                printf("Could not queue a child part.\n");
                CkJsonObject_Dispose(token);
                CkHttp_Dispose(http);
                CkHttpResponse_Dispose(resp);
                CkJsonObject_Dispose(reply);
                CkStringBuilder_Dispose(url);
                CkStringTable_Dispose(paths);
                CkJsonObject_Dispose(part);
                CkStringBuilder_Dispose(childPath);
                return;
            }

            j = j + 1;
        }

        p = p + 1;
    }



    CkJsonObject_Dispose(token);
    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);
    CkJsonObject_Dispose(reply);
    CkStringBuilder_Dispose(url);
    CkStringTable_Dispose(paths);
    CkJsonObject_Dispose(part);
    CkStringBuilder_Dispose(childPath);

    }