Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
Mark a Gmail message as read or archive it
See more GMail REST API Examples
Choose read or archive and supply a Gmail message ID. Marking a message as read removes UNREAD. Archiving removes INBOX while keeping the message available elsewhere in the mailbox. Each run performs the selected action on one message. Removing a label that is already absent is safe to repeat.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. 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;
action: PWideChar;
request: HCkJsonObject;
url: HCkStringBuilder;
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();
action := 'read';
request := CkJsonObject_Create();
if (action = 'read') then
begin
success := CkJsonObject_UpdateString(request,'removeLabelIds[0]','UNREAD');
if (success <> True) then
begin
Memo1.Lines.Add(CkJsonObject__lastErrorText(request));
Exit;
end;
end
else
begin
if (action <> 'archive') then
begin
Memo1.Lines.Add('Choose read or archive.');
Exit;
end;
success := CkJsonObject_UpdateString(request,'removeLabelIds[0]','INBOX');
if (success <> True) then
begin
Memo1.Lines.Add(CkJsonObject__lastErrorText(request));
Exit;
end;
end;
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://gmail.googleapis.com/gmail/v1/users/me/messages/');
CkStringBuilder_Append(url,'GMAIL_MESSAGE_ID');
CkStringBuilder_Append(url,'/modify');
success := CkHttp_HttpJson(http,'POST',CkStringBuilder__getAsString(url),request,'application/json',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,'id') <> 1) or (CkJsonObject_StringOfEquals(reply,'id','',True) = True)) then
begin
Memo1.Lines.Add('Expected id in the response.');
Exit;
end;
Memo1.Lines.Add('Completed ' + action + ' for message ' + CkJsonObject__stringOf(reply,'id'));
CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(request);
CkStringBuilder_Dispose(url);