Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.1.0+

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.

Google documentation

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.

    DECLARE @mailbox nvarchar(4000)
    SELECT @mailbox = 'user@example.com'
    DECLARE @key int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @key OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @key, 'LoadFile', @success OUT, 'gmail-service-account.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load the private service-account key.'
        EXEC @hr = sp_OADestroy @key
        RETURN
      END
    EXEC sp_OAMethod @key, 'StringOfEquals', @iTmp0 OUT, 'type', 'service_account', 1
    EXEC sp_OAMethod @key, 'JsonTypeOf', @iTmp1 OUT, 'private_key'
    IF (@iTmp0 <> 1) or (@iTmp1 <> 1)
      BEGIN

        PRINT 'Expected a Google service-account JSON key.'
        EXEC @hr = sp_OADestroy @key
        RETURN
      END
    DECLARE @auth int
    EXEC @hr = sp_OACreate 'Chilkat.AuthGoogle', @auth OUT

    EXEC sp_OAMethod @key, 'Emit', @sTmp0 OUT
    EXEC sp_OASetProperty @auth, 'JsonKey', @sTmp0
    EXEC sp_OASetProperty @auth, 'SubEmailAddress', @mailbox
    EXEC sp_OASetProperty @auth, 'Scope', 'https://www.googleapis.com/auth/gmail.imap_admin'
    EXEC sp_OASetProperty @auth, 'ExpireNumSeconds', 3600
    DECLARE @connection int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT

    EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
    EXEC sp_OAMethod @connection, 'Connect', @success OUT, 'www.googleapis.com', 443, 1, 10000
    IF @success <> 1
      BEGIN

        PRINT 'Could not establish the Google token connection.'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @auth
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @auth, 'ObtainAccessToken', @success OUT, @connection
    EXEC sp_OAMethod @connection, 'Close', @success OUT, 1000
    IF @success <> 1
      BEGIN

        PRINT 'Check the key, domain-wide delegation client ID/scope, user, and computer clock.'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @auth
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    DECLARE @imap int
    EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT

    EXEC sp_OASetProperty @imap, 'Ssl', 1
    EXEC sp_OASetProperty @imap, 'Port', 993
    EXEC sp_OASetProperty @imap, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @imap, 'AuthMethod', 'XOAUTH2'
    EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.gmail.com'
    IF @success <> 1
      BEGIN

        PRINT 'Could not establish the Gmail IMAP TLS connection.'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @auth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END
    EXEC sp_OAGetProperty @auth, 'AccessToken', @sTmp0 OUT
    EXEC sp_OAMethod @imap, 'Login', @success OUT, @mailbox, @sTmp0
    IF @success <> 1
      BEGIN
        EXEC sp_OAMethod @imap, 'Disconnect', @success OUT

        PRINT 'IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @auth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END
    DECLARE @folders int
    EXEC @hr = sp_OACreate 'Chilkat.Mailboxes', @folders OUT

    EXEC sp_OAMethod @imap, 'MbxList', @success OUT, 0, '', '*', @folders
    IF @success <> 1
      BEGIN
        EXEC sp_OAMethod @imap, 'Logout', @success OUT
        EXEC sp_OAMethod @imap, 'Disconnect', @success OUT

        PRINT 'Could not list Gmail folders.'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @auth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @folders
        RETURN
      END
    --  With gmail.imap_admin, labels hidden by the user's Show in IMAP setting are included.
    DECLARE @i int
    SELECT @i = 0
    EXEC sp_OAGetProperty @folders, 'Count', @iTmp0 OUT
    WHILE (@i < @iTmp0)
      BEGIN
        EXEC sp_OAMethod @folders, 'GetName', @sTmp0 OUT, @i
        PRINT @sTmp0
        SELECT @i = @i + 1
      END
    EXEC sp_OAMethod @imap, 'ExamineMailbox', @success OUT, 'INBOX'
    IF @success <> 1
      BEGIN
        EXEC sp_OAMethod @imap, 'Logout', @success OUT
        EXEC sp_OAMethod @imap, 'Disconnect', @success OUT

        PRINT 'Could not examine the impersonated user''s INBOX.'
        EXEC @hr = sp_OADestroy @key
        EXEC @hr = sp_OADestroy @auth
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @folders
        RETURN
      END

    EXEC sp_OAGetProperty @imap, 'NumMessages', @iTmp0 OUT
    PRINT 'INBOX messages: ' + @iTmp0
    EXEC sp_OAMethod @imap, 'Logout', @success OUT
    EXEC sp_OAMethod @imap, 'Disconnect', @success OUT

    EXEC @hr = sp_OADestroy @key
    EXEC @hr = sp_OADestroy @auth
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @folders


END
GO