Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply
oleobject loo_Email
oleobject loo_Mime
oleobject loo_Request

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Token
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Token.LoadFile("gmail-token.json")
if li_Success <> 1 then
    Write-Debug "Could not read gmail-token.json from the private working directory."
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "An OAuth access token is required."
    destroy loo_Token
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Token.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

//  Use the authorized mailbox or one of its configured send-as addresses.
loo_Email.From = "Sender <sender@example.com>"
li_Success = loo_Email.AddTo("Recipient","recipient@example.net")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    return
end if

loo_Email.Subject = "Sample message"
loo_Email.Charset = "utf-8"
loo_Email.Body = "Hello from the Gmail API."
//  Gmail needs the blind recipient in the submitted MIME; use the normal Chilkat 11.x Bcc behavior.
li_Success = loo_Email.AddBcc("Blind recipient","blind@example.org")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    return
end if

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Email.GetMimeBd(loo_Mime)
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    destroy loo_Mime
    return
end if

loo_Request = create oleobject
li_rc = loo_Request.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Request.UpdateBd("raw","base64url",loo_Mime)
if li_Success <> 1 then
    Write-Debug loo_Request.LastErrorText
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    return
end if

li_Success = loo_Http.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages/send",loo_Request,"application/json",loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "Gmail did not return an HTTP response."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    return
end if

if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
    li_Success = loo_Reply.Load(loo_Resp.BodyStr)
    if li_Success = 1 then
        Write-Debug loo_Reply.StringOf("error.message")
    end if

    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    return
end if

li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected a Gmail JSON response."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    return
end if

if (loo_Reply.JsonTypeOf("id") <> 1) OR (loo_Reply.StringOfEquals("id","",1) = 1) then
    Write-Debug "Expected id in the response."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Email
    destroy loo_Mime
    destroy loo_Request
    return
end if

Write-Debug "Gmail message ID: " + loo_Reply.StringOf("id")


destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_Email
destroy loo_Mime
destroy loo_Request