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#
bool success = false;

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

Chilkat.JsonObject token = new Chilkat.JsonObject();
success = token.LoadFile("gmail-token.json");
if (success != true) {
    Debug.WriteLine("Could not read gmail-token.json from the private working directory.");
    return;
}

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

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = token.StringOf("access_token");
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
Chilkat.JsonObject reply = new Chilkat.JsonObject();
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/");
url.Append("GMAIL_MESSAGE_ID");
url.Append("?format=full");
success = http.HttpNoBody("GET",url.GetAsString(),resp);
if (resp.StatusCode == 0) {
    Debug.WriteLine("Gmail did not return an HTTP response.");
    return;
}

if ((success != true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    Debug.WriteLine("Gmail HTTP " + Convert.ToString(resp.StatusCode));
    success = reply.Load(resp.BodyStr);
    if (success == true) {
        Debug.WriteLine(reply.StringOf("error.message"));
    }

    return;
}

success = reply.Load(resp.BodyStr);
if (success != true) {
    Debug.WriteLine("Expected a Gmail JSON response.");
    return;
}

if (reply.JsonTypeOf("payload") != 3) {
    Debug.WriteLine("Expected a MIME payload.");
    return;
}

//  Queue paths into the original JSON document so any MIME nesting depth is supported.
Chilkat.StringTable paths = new Chilkat.StringTable();
success = paths.Append("payload");
if (success != true) {
    Debug.WriteLine("Could not queue the root part.");
    return;
}

Chilkat.JsonObject part = new Chilkat.JsonObject();
Chilkat.StringBuilder childPath = new Chilkat.StringBuilder();
int p = 0;
while ((p < paths.Count)) {
    success = reply.ObjectOf2(paths.StringAt(p),part);
    if (success != true) {
        Debug.WriteLine("Could not read a MIME part.");
        return;
    }

    Debug.WriteLine("Part: " + part.StringOf("mimeType") + "  bytes: " + Convert.ToString(part.IntOf("body.size")));
    if (part.JsonTypeOf("filename") == 1) {
        if (part.StringOfEquals("filename","",true) != true) {
            Debug.WriteLine("Filename: " + part.StringOf("filename"));
        }

    }

    if (part.JsonTypeOf("body.attachmentId") == 1) {
        Debug.WriteLine("Attachment ID: " + part.StringOf("body.attachmentId"));
    }
    else {
        if (part.JsonTypeOf("body.data") == 1) {
            Debug.WriteLine("Body data is available in this part.");
        }

    }

    int j = 0;
    while ((j < part.SizeOfArray("parts"))) {
        childPath.SetString(paths.StringAt(p));
        childPath.Append(".parts[");
        childPath.AppendInt(j);
        childPath.Append("]");
        success = paths.Append(childPath.GetAsString());
        if (success != true) {
            Debug.WriteLine("Could not queue a child part.");
            return;
        }

        j = j + 1;
    }

    p = p + 1;
}