Sample code for 30+ languages & platforms
PowerBuilder

OAuth2 for a GMail using a P12 Service Account Key

See more GMail REST API Examples

Demonstrates how to use GMail with OAuth2 for a service account within a Google Workspace Account where your email domain is custom (e.g., @yourcompany.com).

Note: This example does not work for Personal Google Accounts where the email domain is @gmail.com

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Cert
string ls_Iss
string ls_Scope
string ls_Oauth_sub
integer li_NumSec
string ls_AccessToken
oleobject loo_Mailman
oleobject loo_Email

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// --------------------------------------------------------------------------------
// For a step-by-step guide for setting up your Google Workspace service account,
// see Setup Google Workspace Account for Sending SMTP GMail from a Service Account
// --------------------------------------------------------------------------------
// Begin by loading your Google service account key (.p12)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12","notasecret")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Http
    destroy loo_Cert
    return
end if

// The ISS is your service account email address ending in gserviceaccount.com.
ls_Iss = "chilkatsvc@chilkat25.iam.gserviceaccount.com"

// The scope is always the following string:
ls_Scope = "https://mail.google.com/"

// The sub is your company email address
ls_Oauth_sub = "bob@yourcompany.com"

// The access token is valid for this number of seconds.
li_NumSec = 3600

ls_AccessToken = loo_Http.G_SvcOauthAccessToken(ls_Iss,ls_Scope,ls_Oauth_sub,li_NumSec,loo_Cert)
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Cert
    return
else
    Write-Debug "access token: " + ls_AccessToken
end if

// The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.

// -----------------------------------------------------------------------
loo_Mailman = create oleobject
li_rc = loo_Mailman.ConnectToNewObject("Chilkat.MailMan")

// Set the properties for the GMail SMTP server:
loo_Mailman.SmtpHost = "smtp.gmail.com"
loo_Mailman.SmtpPort = 587
loo_Mailman.StartTLS = 1

loo_Mailman.SmtpUsername = "bob@yourcompany.com"
loo_Mailman.OAuth2AccessToken = ls_AccessToken

// Create a new email object
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.Subject = "This is a test"
loo_Email.Body = "This is a test"
loo_Email.From = "Bob <bob@yourcompany.com>"
li_Success = loo_Email.AddTo("Recipient","recipient@example.com")
// To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.

li_Success = loo_Mailman.SendEmail(loo_Email)
if li_Success <> 1 then
    Write-Debug loo_Mailman.LastErrorText
    destroy loo_Http
    destroy loo_Cert
    destroy loo_Mailman
    destroy loo_Email
    return
end if

li_Success = loo_Mailman.CloseSmtpConnection()
if li_Success <> 1 then
    Write-Debug "Connection to SMTP server not closed cleanly."
end if

Write-Debug "Successfully sent email using Gmail with a service account key."


destroy loo_Http
destroy loo_Cert
destroy loo_Mailman
destroy loo_Email