POP3 Implicit TLS: Gmail OAuth2 on Port 995
Authenticate to Gmail on port 995 with implicit TLS and inspect headers for up to ten currently available POP messages. UIDLs stay as strings. With headerOnly=true, a zero line-count argument retrieves headers plus the first body line. The example displays only headers and issues TOP, neither RETR nor DELE.
Enable POP in Gmail settings and review the option controlling Gmail's copy after access. Delivery windows, history, and batching mean available POP mail is not the entire Gmail mailbox. Use IMAP for folder/label access and synchronization.
First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.
Implicit TLS starts when the TCP connection opens. It is different from STLS on port 110. The historical password-based Gmail sample has been replaced with OAuth2.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat SQL Server Downloads
-- 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 @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'gmail-mail-token.json'
IF @success <> 1
BEGIN
PRINT 'Could not read gmail-mail-token.json from the private working directory.'
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'An OAuth access token is required. Authorize or refresh the account first.'
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @mailbox nvarchar(4000)
SELECT @mailbox = 'user@example.com'
DECLARE @mailman int
EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT
EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.gmail.com'
EXEC sp_OASetProperty @mailman, 'MailPort', 995
EXEC sp_OASetProperty @mailman, 'PopSsl', 1
EXEC sp_OASetProperty @mailman, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @mailman, 'PopUsername', @mailbox
EXEC sp_OASetProperty @mailman, 'PopPassword', ''
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @mailman, 'OAuth2AccessToken', @sTmp0
EXEC sp_OAMethod @mailman, 'Pop3Connect', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not establish the Gmail POP3 TLS connection.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @mailman
RETURN
END
EXEC sp_OAMethod @mailman, 'Pop3Authenticate', @success OUT
IF @success <> 1
BEGIN
EXEC sp_OAMethod @mailman, 'Pop3EndSessionNoQuit', @success OUT
PRINT 'POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @mailman
RETURN
END
DECLARE @uidls int
EXEC @hr = sp_OACreate 'Chilkat.StringTable', @uidls OUT
EXEC sp_OAMethod @mailman, 'FetchUidls', @success OUT, @uidls
IF @success <> 1
BEGIN
EXEC sp_OAMethod @mailman, 'Pop3EndSessionNoQuit', @success OUT
PRINT 'Could not list available POP UIDLs.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @uidls
RETURN
END
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @uidls, 'Count', @iTmp0 OUT
WHILE ((@i < @iTmp0) and (@i < 10))
BEGIN
DECLARE @uidl nvarchar(4000)
EXEC sp_OAMethod @uidls, 'StringAt', @uidl OUT, @i
-- With headerOnly=true, zero requests headers plus the first body line.
-- Chilkat uses TOP rather than RETR; this example does not issue DELE.
EXEC sp_OAMethod @mailman, 'FetchByUidl', @success OUT, @uidl, 1, 0, @email
IF @success <> 1
BEGIN
EXEC sp_OAMethod @mailman, 'Pop3EndSessionNoQuit', @success OUT
PRINT 'Could not read message headers.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @uidls
EXEC @hr = sp_OADestroy @email
RETURN
END
PRINT 'UIDL: ' + @uidl
EXEC sp_OAGetProperty @email, 'From', @sTmp0 OUT
PRINT 'From: ' + @sTmp0
EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT
PRINT 'Subject: ' + @sTmp0
SELECT @i = @i + 1
END
EXEC sp_OAMethod @mailman, 'Pop3EndSession', @success OUT
IF @success <> 1
BEGIN
PRINT 'Headers were read, but closing the POP session failed.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @uidls
EXEC @hr = sp_OADestroy @email
RETURN
END
PRINT 'Headers inspected: ' + @i
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @mailman
EXEC @hr = sp_OADestroy @uidls
EXEC @hr = sp_OADestroy @email
END
GO