Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
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 Delphi ActiveX Downloads
var
success: Integer;
token: TChilkatJsonObject;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
reply: TChilkatJsonObject;
url: TChilkatStringBuilder;
paths: TChilkatStringTable;
part: TChilkatJsonObject;
childPath: TChilkatStringBuilder;
p: Integer;
j: Integer;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := TChilkatJsonObject.Create(Self);
success := token.LoadFile('gmail-token.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not read gmail-token.json from the private working directory.');
Exit;
end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',1) = 1)) then
begin
Memo1.Lines.Add('An OAuth access token is required.');
Exit;
end;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.AuthToken := token.StringOf('access_token');
resp := TChilkatHttpResponse.Create(Self);
reply := TChilkatJsonObject.Create(Self);
url := TChilkatStringBuilder.Create(Self);
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.ControlInterface);
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add('Gmail did not return an HTTP response.');
Exit;
end;
if ((success <> 1) or (resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
begin
Memo1.Lines.Add('Gmail HTTP ' + IntToStr(resp.StatusCode));
success := reply.Load(resp.BodyStr);
if (success = 1) then
begin
Memo1.Lines.Add(reply.StringOf('error.message'));
end;
Exit;
end;
success := reply.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a Gmail JSON response.');
Exit;
end;
if (reply.JsonTypeOf('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 := TChilkatStringTable.Create(Self);
success := paths.Append('payload');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not queue the root part.');
Exit;
end;
part := TChilkatJsonObject.Create(Self);
childPath := TChilkatStringBuilder.Create(Self);
p := 0;
while (p < paths.Count) do
begin
success := reply.ObjectOf2(paths.StringAt(p),part.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not read a MIME part.');
Exit;
end;
Memo1.Lines.Add('Part: ' + part.StringOf('mimeType') + ' bytes: ' + IntToStr(part.IntOf('body.size')));
if (part.JsonTypeOf('filename') = 1) then
begin
if (part.StringOfEquals('filename','',1) <> 1) then
begin
Memo1.Lines.Add('Filename: ' + part.StringOf('filename'));
end;
end;
if (part.JsonTypeOf('body.attachmentId') = 1) then
begin
Memo1.Lines.Add('Attachment ID: ' + part.StringOf('body.attachmentId'));
end
else
begin
if (part.JsonTypeOf('body.data') = 1) then
begin
Memo1.Lines.Add('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 <> 1) then
begin
Memo1.Lines.Add('Could not queue a child part.');
Exit;
end;
j := j + 1;
end;
p := p + 1;
end;