Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
reply: HCkJsonObject;
url: HCkStringBuilder;
paths: HCkStringTable;
part: HCkJsonObject;
childPath: HCkStringBuilder;
p: Integer;
j: Integer;

begin
success := False;

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

token := CkJsonObject_Create();
success := CkJsonObject_LoadFile(token,'gmail-token.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read gmail-token.json from the private working directory.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(token,'access_token') <> 1) or (CkJsonObject_StringOfEquals(token,'access_token','',True) = True)) then
  begin
    Memo1.Lines.Add('An OAuth access token is required.');
    Exit;
  end;
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) then
  begin
    Memo1.Lines.Add('Gmail did not return an HTTP response.');
    Exit;
  end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    Memo1.Lines.Add('Gmail HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
    if (success = True) then
      begin
        Memo1.Lines.Add(CkJsonObject__stringOf(reply,'error.message'));
      end;
    Exit;
  end;
success := CkJsonObject_Load(reply,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a Gmail JSON response.');
    Exit;
  end;
if (CkJsonObject_JsonTypeOf(reply,'payload') <> 3) then
  begin
    Memo1.Lines.Add('Expected a MIME payload.');
    Exit;
  end;
//  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) then
  begin
    Memo1.Lines.Add('Could not queue the root part.');
    Exit;
  end;
part := CkJsonObject_Create();
childPath := CkStringBuilder_Create();
p := 0;
while (p < CkStringTable_getCount(paths)) do
  begin
    success := CkJsonObject_ObjectOf2(reply,CkStringTable__stringAt(paths,p),part);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not read a MIME part.');
        Exit;
      end;
    Memo1.Lines.Add('Part: ' + CkJsonObject__stringOf(part,'mimeType') + '  bytes: ' + IntToStr(CkJsonObject_IntOf(part,'body.size')));
    if (CkJsonObject_JsonTypeOf(part,'filename') = 1) then
      begin
        if (CkJsonObject_StringOfEquals(part,'filename','',True) <> True) then
          begin
            Memo1.Lines.Add('Filename: ' + CkJsonObject__stringOf(part,'filename'));
          end;
      end;
    if (CkJsonObject_JsonTypeOf(part,'body.attachmentId') = 1) then
      begin
        Memo1.Lines.Add('Attachment ID: ' + CkJsonObject__stringOf(part,'body.attachmentId'));
      end
    else
      begin
        if (CkJsonObject_JsonTypeOf(part,'body.data') = 1) then
          begin
            Memo1.Lines.Add('Body data is available in this part.');
          end;
      end;
    j := 0;
    while (j < CkJsonObject_SizeOfArray(part,'parts')) do
      begin
        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) then
          begin
            Memo1.Lines.Add('Could not queue a child part.');
            Exit;
          end;
        j := j + 1;
      end;

    p := p + 1;
  end;

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