Sample code for 30+ languages & platforms
PowerBuilder

Send GMail using REST API

See more GMail REST API Examples

Demonstrates how to send an email using the GMail REST API.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Email
string ls_Cid
oleobject loo_SbHtml
integer li_NumReplacements
oleobject loo_SbMime
oleobject loo_Json
oleobject loo_Http
string ls_Url
oleobject loo_Resp

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Use the Chilkat Email API to create or load an email.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
if li_rc < 0 then
    destroy loo_Email
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// The From name/address don't need to be specified. 
// GMail will automatically use your Gmail address if the following 2 lines are omitted.
loo_Email.FromName = "My Name"
loo_Email.FromAddress = "my_account_name@gmail.com"

loo_Email.AddTo("John Smith","somebody@somewhere.com")
// To add more recipients, make additional calls to AddTo or AddCC, one per recipient...

loo_Email.Subject = "This is a test GMail email created using Chilkat."
loo_Email.Charset = "utf-8"
loo_Email.AddPlainTextAlternativeBody("This is a test")

// Create an HTML email body with an embedded image.
ls_Cid = loo_Email.AddRelatedFile("qa_data/jpg/starfish.jpg")
loo_SbHtml = create oleobject
li_rc = loo_SbHtml.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbHtml.Append("<html><body>This is a <b>test</b><br><img src=~"cid:STARFISH_CID~"/></body></html>")
li_NumReplacements = loo_SbHtml.Replace("STARFISH_CID",ls_Cid)

loo_Email.AddHtmlAlternativeBody(loo_SbHtml.GetAsString())

// OK.. we now have an HTML email with an embedded JPG image
// We'll need to get the full MIME of the email encoded to a base64url string.
loo_SbMime = create oleobject
li_rc = loo_SbMime.ConnectToNewObject("Chilkat.StringBuilder")

loo_Email.GetMimeSb(loo_SbMime)
loo_SbMime.Encode("base64url","utf-8")

// The body of the HTTP request will be JSON..
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateSb("raw",loo_SbMime)

// Send the email...
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.AuthToken = "GMAIL-ACCESS-TOKEN"

ls_Url = "https://www.googleapis.com/gmail/v1/users/me/messages/send"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpJson("POST",ls_Url,loo_Json,"application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Email
    destroy loo_SbHtml
    destroy loo_SbMime
    destroy loo_Json
    destroy loo_Http
    destroy loo_Resp
    return
end if

// A status code of 200 indicates success.
Write-Debug "Response status code: " + string(loo_Resp.StatusCode)
Write-Debug "Response body:"
Write-Debug loo_Resp.BodyStr

// The response body contains JSON.
// Use the online tool at Generate JSON Parsing Code
// to generate JSON parsing code.

// A sample successful JSON response:

// {
//  "id": "166f0d4ac39e50bf",
//  "threadId": "166f0d4ac39e50bf",
//  "labelIds": [
//   "SENT"
//  ]
// }


destroy loo_Email
destroy loo_SbHtml
destroy loo_SbMime
destroy loo_Json
destroy loo_Http
destroy loo_Resp