Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

VB.NET Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(VB.NET) 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

For more information, see https://www.chilkatsoft.com/google_workspace_setup_smtp_gmail.asp

Chilkat .NET Downloads

Chilkat .NET Framework

Chilkat for .NET Core

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

Dim http As New 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)
Dim cert As New Chilkat.Cert
Dim success As Boolean = cert.LoadPfxFile("c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12","notasecret")
If (success <> True) Then
    Debug.WriteLine(cert.LastErrorText)
    Exit Sub
End If


' The ISS is your service account email address ending in gserviceaccount.com.
Dim iss As String = "chilkatsvc@chilkat25.iam.gserviceaccount.com"

' The scope is always the following string:
Dim scope As String = "https://mail.google.com/"

' The sub is your company email address
Dim oauth_sub As String = "bob@yourcompany.com"

' The access token is valid for this number of seconds.
Dim numSec As Integer = 3600

Dim accessToken As String = http.G_SvcOauthAccessToken(iss,scope,oauth_sub,numSec,cert)
If (http.LastMethodSuccess <> True) Then
    Debug.WriteLine(http.LastErrorText)
    Exit Sub
Else
    Debug.WriteLine("access token: " & 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.


' -----------------------------------------------------------------------
Dim mailman As New Chilkat.MailMan

' Set the properties for the GMail SMTP server:
mailman.SmtpHost = "smtp.gmail.com"
mailman.SmtpPort = 587
mailman.StartTLS = True

mailman.SmtpUsername = "bob@yourcompany.com"
mailman.OAuth2AccessToken = accessToken

' Create a new email object
Dim email As New Chilkat.Email

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

success = mailman.SendEmail(email)
If (success <> True) Then
    Debug.WriteLine(mailman.LastErrorText)
    Exit Sub
End If


success = mailman.CloseSmtpConnection()
If (success <> True) Then
    Debug.WriteLine("Connection to SMTP server not closed cleanly.")
End If


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

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.