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

Send email to blind-copy recipients with Gmail

See more GMail REST API Examples

Live send. Replace all sample addresses. Gmail takes its recipients from the submitted MIME To, Cc, and Bcc headers. Use AddBcc on a new Email and keep the Bcc header when serializing it for Gmail. Treat the submission as private because it contains the blind-copy addresses.

Authorize the application with https://www.googleapis.com/auth/gmail.send. 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;
email: HCkEmail;
mime: HCkBinData;
request: HCkJsonObject;

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();
email := CkEmail_Create();
//  Use the authorized mailbox or one of its configured send-as addresses.
CkEmail_putFrom(email,'Sender <sender@example.com>');
success := CkEmail_AddTo(email,'Recipient','recipient@example.net');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
CkEmail_putSubject(email,'Sample message');
CkEmail_putCharset(email,'utf-8');
CkEmail_putBody(email,'Hello from the Gmail API.');
//  Gmail needs the blind recipient in the submitted MIME; use the normal Chilkat 11.x Bcc behavior.
success := CkEmail_AddBcc(email,'Blind recipient','blind@example.org');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
mime := CkBinData_Create();
success := CkEmail_GetMimeBd(email,mime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
request := CkJsonObject_Create();
success := CkJsonObject_UpdateBd(request,'raw','base64url',mime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(request));
    Exit;
  end;
success := CkHttp_HttpJson(http,'POST','https://gmail.googleapis.com/gmail/v1/users/me/messages/send',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('Gmail message ID: ' + CkJsonObject__stringOf(reply,'id'));

CkJsonObject_Dispose(token);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkEmail_Dispose(email);
CkBinData_Dispose(mime);
CkJsonObject_Dispose(request);