Gmail IMAP: Workspace Domain-Wide Delegation
See more GMail SMTP/IMAP/POP Examples
Use a JSON service-account key to impersonate a Workspace user for IMAP with https://www.googleapis.com/auth/gmail.imap_admin. A Workspace administrator must authorize the service account's numeric OAuth client ID for this exact scope in domain-wide delegation. The impersonated user must have an active Gmail mailbox.
This IMAP-only scope reveals labels hidden by Show in IMAP and messages excluded by the user's folder-size limit. It does not authorize SMTP or POP and is not available for personal Gmail accounts or ordinary user-consent flows. Workspace administrative access policies still apply.
Protect the private key and limit who can execute the delegated application. The signed JWT exchange produces a short-lived token without a refresh token; obtain another when necessary and reconnect. Keep the computer clock accurate. This example lists folders and examines INBOX read-only without printing credentials.
Chilkat VB.NET Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim mailbox As String = "user@example.com"
Dim key As New Chilkat.JsonObject
success = key.LoadFile("gmail-service-account.json")
If (success <> True) Then
Debug.WriteLine("Could not load the private service-account key.")
Exit Sub
End If
If ((key.StringOfEquals("type","service_account",True) <> True) Or (key.JsonTypeOf("private_key") <> 1)) Then
Debug.WriteLine("Expected a Google service-account JSON key.")
Exit Sub
End If
Dim auth As New Chilkat.AuthGoogle
auth.JsonKey = key.Emit()
auth.SubEmailAddress = mailbox
auth.Scope = "https://www.googleapis.com/auth/gmail.imap_admin"
auth.ExpireNumSeconds = 3600
Dim connection As New Chilkat.Socket
connection.RequireSslCertVerify = True
success = connection.Connect("www.googleapis.com",443,True,10000)
If (success <> True) Then
Debug.WriteLine("Could not establish the Google token connection.")
Exit Sub
End If
success = auth.ObtainAccessToken(connection)
connection.Close(1000)
If (success <> True) Then
Debug.WriteLine("Check the key, domain-wide delegation client ID/scope, user, and computer clock.")
Exit Sub
End If
Dim imap As New Chilkat.Imap
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> True) Then
Debug.WriteLine("Could not establish the Gmail IMAP TLS connection.")
Exit Sub
End If
success = imap.Login(mailbox,auth.AccessToken)
If (success <> True) Then
imap.Disconnect()
Debug.WriteLine("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
Exit Sub
End If
Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Could not list Gmail folders.")
Exit Sub
End If
' With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
Dim i As Integer = 0
While (i < folders.Count)
Debug.WriteLine(folders.GetName(i))
i = i + 1
End While
success = imap.ExamineMailbox("INBOX")
If (success <> True) Then
imap.Logout()
imap.Disconnect()
Debug.WriteLine("Could not examine the impersonated user's INBOX.")
Exit Sub
End If
Debug.WriteLine("INBOX messages: " & imap.NumMessages)
imap.Logout()
imap.Disconnect()