Tcl
Tcl
OAuth2 for a GMail using a P12 Service Account Key
See more GMail REST API Examples
Demonstrates how to use GMail with OAuth2 for aservice 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 Tcl Downloads
load ./chilkat.dll
set success 0
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set http [new_CkHttp]
# --------------------------------------------------------------------------------
# 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)
set cert [new_CkCert]
set success [CkCert_LoadPfxFile $cert "c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12" "notasecret"]
if {$success != 1} then {
puts [CkCert_lastErrorText $cert]
delete_CkHttp $http
delete_CkCert $cert
exit
}
# The ISS is your service account email address ending in gserviceaccount.com.
set iss "chilkatsvc@chilkat25.iam.gserviceaccount.com"
# The scope is always the following string:
set scope "https://mail.google.com/"
# The sub is your company email address
set oauth_sub "bob@yourcompany.com"
# The access token is valid for this number of seconds.
set numSec 3600
set accessToken [CkHttp_g_SvcOauthAccessToken $http $iss $scope $oauth_sub $numSec $cert]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkCert $cert
exit
} else {
puts "access token: $accessToken"
}
# The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.
# -----------------------------------------------------------------------
set mailman [new_CkMailMan]
# Set the properties for the GMail SMTP server:
CkMailMan_put_SmtpHost $mailman "smtp.gmail.com"
CkMailMan_put_SmtpPort $mailman 587
CkMailMan_put_StartTLS $mailman 1
CkMailMan_put_SmtpUsername $mailman "bob@yourcompany.com"
CkMailMan_put_OAuth2AccessToken $mailman $accessToken
# Create a new email object
set email [new_CkEmail]
CkEmail_put_Subject $email "This is a test"
CkEmail_put_Body $email "This is a test"
CkEmail_put_From $email "Bob <bob@yourcompany.com>"
set success [CkEmail_AddTo $email "Recipient" "recipient@example.com"]
# To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.
set success [CkMailMan_SendEmail $mailman $email]
if {$success != 1} then {
puts [CkMailMan_lastErrorText $mailman]
delete_CkHttp $http
delete_CkCert $cert
delete_CkMailMan $mailman
delete_CkEmail $email
exit
}
set success [CkMailMan_CloseSmtpConnection $mailman]
if {$success != 1} then {
puts "Connection to SMTP server not closed cleanly."
}
puts "Successfully sent email using Gmail with a service account key."
delete_CkHttp $http
delete_CkCert $cert
delete_CkMailMan $mailman
delete_CkEmail $email