Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loCert
LOCAL lcIss
LOCAL lcScope
LOCAL lcOauth_sub
LOCAL lnNumSec
LOCAL lcAccessToken
LOCAL loMailman
LOCAL loEmail

lnSuccess = 0

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

loHttp = CreateObject('Chilkat.Http')

* --------------------------------------------------------------------------------
* 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)
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile("c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12","notasecret")
IF (lnSuccess <> 1) THEN
    ? loCert.LastErrorText
    RELEASE loHttp
    RELEASE loCert
    CANCEL
ENDIF

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

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

* The sub is your company email address
lcOauth_sub = "bob@yourcompany.com"

* The access token is valid for this number of seconds.
lnNumSec = 3600

lcAccessToken = loHttp.G_SvcOauthAccessToken(lcIss,lcScope,lcOauth_sub,lnNumSec,loCert)
IF (loHttp.LastMethodSuccess <> 1) THEN
    ? loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loCert
    CANCEL
ELSE
    ? "access token: " + lcAccessToken
ENDIF

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

* -----------------------------------------------------------------------
loMailman = CreateObject('Chilkat.MailMan')

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

loMailman.SmtpUsername = "bob@yourcompany.com"
loMailman.OAuth2AccessToken = lcAccessToken

* Create a new email object
loEmail = CreateObject('Chilkat.Email')

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

lnSuccess = loMailman.SendEmail(loEmail)
IF (lnSuccess <> 1) THEN
    ? loMailman.LastErrorText
    RELEASE loHttp
    RELEASE loCert
    RELEASE loMailman
    RELEASE loEmail
    CANCEL
ENDIF

lnSuccess = loMailman.CloseSmtpConnection()
IF (lnSuccess <> 1) THEN
    ? "Connection to SMTP server not closed cleanly."
ENDIF

? "Successfully sent email using Gmail with a service account key."

RELEASE loHttp
RELEASE loCert
RELEASE loMailman
RELEASE loEmail