Lazarus Pascal Requires Chilkat v11.0.0+
Lazarus Pascal
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.
Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.StringBuilder,
Chilkat.StringTable,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
token: TJsonObject;
http: THttp;
resp: THttpResponse;
reply: TJsonObject;
url: TStringBuilder;
paths: TStringTable;
part: TJsonObject;
childPath: TStringBuilder;
p: Integer;
j: Integer;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := TJsonObject.Create;
success := token.LoadFile('gmail-token.json');
if (success <> True) then
begin
WriteLn('Could not read gmail-token.json from the private working directory.');
Exit;
end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',True) = True)) then
begin
WriteLn('An OAuth access token is required.');
Exit;
end;
http := THttp.Create;
http.RequireSslCertVerify := True;
http.AuthToken := token.StringOf('access_token');
resp := THttpResponse.Create;
reply := TJsonObject.Create;
url := TStringBuilder.Create;
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) then
begin
WriteLn('Gmail did not return an HTTP response.');
Exit;
end;
if ((success <> True) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
WriteLn('Gmail HTTP ' + resp.StatusCode);
success := reply.Load(resp.BodyStr);
if (success = True) then
begin
WriteLn(reply.StringOf('error.message'));
end;
Exit;
end;
success := reply.Load(resp.BodyStr);
if (success <> True) then
begin
WriteLn('Expected a Gmail JSON response.');
Exit;
end;
if (reply.JsonTypeOf('payload') <> 3) then
begin
WriteLn('Expected a MIME payload.');
Exit;
end;
// Queue paths into the original JSON document so any MIME nesting depth is supported.
paths := TStringTable.Create;
success := paths.Append('payload');
if (success <> True) then
begin
WriteLn('Could not queue the root part.');
Exit;
end;
part := TJsonObject.Create;
childPath := TStringBuilder.Create;
p := 0;
while (p < paths.Count) do
begin
success := reply.ObjectOf2(paths.StringAt(p),part);
if (success <> True) then
begin
WriteLn('Could not read a MIME part.');
Exit;
end;
WriteLn('Part: ' + part.StringOf('mimeType') + ' bytes: ' + part.IntOf('body.size'));
if (part.JsonTypeOf('filename') = 1) then
begin
if (part.StringOfEquals('filename','',True) <> True) then
begin
WriteLn('Filename: ' + part.StringOf('filename'));
end;
end;
if (part.JsonTypeOf('body.attachmentId') = 1) then
begin
WriteLn('Attachment ID: ' + part.StringOf('body.attachmentId'));
end
else
begin
if (part.JsonTypeOf('body.data') = 1) then
begin
WriteLn('Body data is available in this part.');
end;
end;
j := 0;
while (j < part.SizeOfArray('parts')) do
begin
childPath.SetString(paths.StringAt(p));
childPath.Append('.parts[');
childPath.AppendInt(j);
childPath.Append(']');
success := paths.Append(childPath.GetAsString());
if (success <> True) then
begin
WriteLn('Could not queue a child part.');
Exit;
end;
j := j + 1;
end;
p := p + 1;
end;
token.Free;
http.Free;
resp.Free;
reply.Free;
url.Free;
paths.Free;
part.Free;
childPath.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.