Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
Read selected headers for a page of Gmail messages
See more GMail REST API Examples
List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.
Authorize the application with https://www.googleapis.com/auth/gmail.metadata. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Delphi DLL Downloads
var
success: Boolean;
token: HCkJsonObject;
http: HCkHttp;
resp: HCkHttpResponse;
reply: HCkJsonObject;
ids: HCkStringTable;
count: Integer;
i: Integer;
url: HCkStringBuilder;
h: 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();
success := CkHttp_HttpNoBody(http,'GET','https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5',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;
ids := CkStringTable_Create();
count := CkJsonObject_SizeOfArray(reply,'messages');
i := 0;
while (i < count) do
begin
CkJsonObject_putI(reply,i);
success := CkStringTable_Append(ids,CkJsonObject__stringOf(reply,'messages[i].id'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not retain a message ID.');
Exit;
end;
i := i + 1;
end;
url := CkStringBuilder_Create();
i := 0;
while (i < CkStringTable_getCount(ids)) do
begin
CkStringBuilder_SetString(url,'https://gmail.googleapis.com/gmail/v1/users/me/messages/');
CkStringBuilder_Append(url,CkStringTable__stringAt(ids,i));
CkStringBuilder_Append(url,'?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date');
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;
Memo1.Lines.Add('Message: ' + CkStringTable__stringAt(ids,i));
h := 0;
while (h < CkJsonObject_SizeOfArray(reply,'payload.headers')) do
begin
CkJsonObject_putI(reply,h);
Memo1.Lines.Add(CkJsonObject__stringOf(reply,'payload.headers[i].name') + ': ' + CkJsonObject__stringOf(reply,'payload.headers[i].value'));
h := h + 1;
end;
i := i + 1;
end;
Memo1.Lines.Add('Messages inspected: ' + IntToStr(CkStringTable_getCount(ids)));
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkStringTable_Dispose(ids);
CkStringBuilder_Dispose(url);