Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.0.0+

Gmail IMAP: Workspace Service Account with a JSON Key

Use a service-account JSON key with Google Workspace domain-wide delegation. A Workspace administrator must authorize the service account's numeric OAuth client ID for https://mail.google.com/. Set SubEmailAddress to the Workspace user whose active Gmail mailbox is being accessed, and use that same address for IMAP login.

A service account has no Gmail mailbox of its own; leaving the impersonated user empty does not grant access to a personal Gmail account. The signed JWT exchange yields a short-lived token without a refresh token. Obtain another when needed, protect the private key, and keep the computer clock accurate.

This example lists visible folders and examines INBOX read-only. For the different visibility rules of the IMAP-only administrative scope, see gmail.imap_admin delegation.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

mailbox = "user@example.com"
set key = Server.CreateObject("Chilkat.JsonObject")
success = key.LoadFile("gmail-service-account.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load the private service-account key.") & "</pre>"
    Response.End
End If

If ((key.StringOfEquals("type","service_account",1) <> 1) Or (key.JsonTypeOf("private_key") <> 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a Google service-account JSON key.") & "</pre>"
    Response.End
End If

set auth = Server.CreateObject("Chilkat.AuthGoogle")
auth.JsonKey = key.Emit()
auth.SubEmailAddress = mailbox
auth.Scope = "https://mail.google.com/"
auth.ExpireNumSeconds = 3600
set connection = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
success = connection.Connect("www.googleapis.com",443,1,10000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Google token connection.") & "</pre>"
    Response.End
End If

success = auth.ObtainAccessToken(connection)
success = connection.Close(1000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Check the key, domain-wide delegation client ID/scope, user, and computer clock.") & "</pre>"
    Response.End
End If

set imap = Server.CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Gmail IMAP TLS connection.") & "</pre>"
    Response.End
End If

success = imap.Login(mailbox,auth.AccessToken)
If (success <> 1) Then
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.") & "</pre>"
    Response.End
End If

set folders = Server.CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not list Gmail folders.") & "</pre>"
    Response.End
End If

'  List the labels visible through ordinary full-mail IMAP access.
i = 0
Do While (i < folders.Count)
    Response.Write "<pre>" & Server.HTMLEncode( folders.GetName(i)) & "</pre>"
    i = i + 1
Loop
success = imap.ExamineMailbox("INBOX")
If (success <> 1) Then
    success = imap.Logout()
    success = imap.Disconnect()
    Response.Write "<pre>" & Server.HTMLEncode( "Could not examine the impersonated user's INBOX.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "INBOX messages: " & imap.NumMessages) & "</pre>"
success = imap.Logout()
success = imap.Disconnect()

%>
</body>
</html>