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

Download a Gmail attachment by ID

See more GMail REST API Examples

Use a Gmail message ID and a body.attachmentId from the same message. Find the attachment ID by inspecting its MIME parts. Decode the returned data as base64url, check the decoded byte count, and save it to an application-chosen path. A part containing body.data can be decoded directly from the full message.

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;
data: HCkBinData;

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,'/attachments/');
CkStringBuilder_Append(url,'GMAIL_ATTACHMENT_ID');
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,'data') <> 1) or (CkJsonObject_JsonTypeOf(reply,'size') <> 2)) then
  begin
    Memo1.Lines.Add('Expected attachment data and a byte count.');
    Exit;
  end;
data := CkBinData_Create();
if (CkJsonObject_StringOfEquals(reply,'data','',True) <> True) then
  begin
    success := CkJsonObject_BytesOf(reply,'data','base64url',data);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not decode the attachment.');
        Exit;
      end;
  end;
if (CkBinData_getNumBytes(data) <> CkJsonObject_IntOf(reply,'size')) then
  begin
    Memo1.Lines.Add('Attachment size mismatch.');
    Exit;
  end;
success := CkBinData_WriteFile(data,'downloaded-attachment.bin');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not save the attachment.');
    Exit;
  end;
Memo1.Lines.Add('Saved ' + IntToStr(CkBinData_getNumBytes(data)) + ' bytes.');

CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkStringBuilder_Dispose(url);
CkBinData_Dispose(data);