Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; --------------------------------------------------------------------------------
    ; 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)
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12","notasecret")
    If success <> 1
        Debug CkCert::ckLastErrorText(cert)
        CkHttp::ckDispose(http)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; The ISS is your service account email address ending in gserviceaccount.com.
    iss.s = "chilkatsvc@chilkat25.iam.gserviceaccount.com"

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

    ; The sub is your company email address
    oauth_sub.s = "bob@yourcompany.com"

    ; The access token is valid for this number of seconds.
    numSec.i = 3600

    accessToken.s = CkHttp::ckG_SvcOauthAccessToken(http,iss,scope,oauth_sub,numSec,cert)
    If CkHttp::ckLastMethodSuccess(http) <> 1
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkCert::ckDispose(cert)
        ProcedureReturn
    Else
        Debug "access token: " + accessToken
    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.

    ; -----------------------------------------------------------------------
    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Set the properties for the GMail SMTP server:
    CkMailMan::setCkSmtpHost(mailman, "smtp.gmail.com")
    CkMailMan::setCkSmtpPort(mailman, 587)
    CkMailMan::setCkStartTLS(mailman, 1)

    CkMailMan::setCkSmtpUsername(mailman, "bob@yourcompany.com")
    CkMailMan::setCkOAuth2AccessToken(mailman, accessToken)

    ; Create a new email object
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "This is a test")
    CkEmail::setCkBody(email, "This is a test")
    CkEmail::setCkFrom(email, "Bob <bob@yourcompany.com>")
    success = CkEmail::ckAddTo(email,"Recipient","recipient@example.com")
    ; To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.

    success = CkMailMan::ckSendEmail(mailman,email)
    If success <> 1
        Debug CkMailMan::ckLastErrorText(mailman)
        CkHttp::ckDispose(http)
        CkCert::ckDispose(cert)
        CkMailMan::ckDispose(mailman)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    success = CkMailMan::ckCloseSmtpConnection(mailman)
    If success <> 1
        Debug "Connection to SMTP server not closed cleanly."
    EndIf

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


    CkHttp::ckDispose(http)
    CkCert::ckDispose(cert)
    CkMailMan::ckDispose(mailman)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure