Gmail POP3: Read Message Headers with OAuth2
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.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loMailman
LOCAL loUidls
LOCAL loEmail
LOCAL i
LOCAL lcUidl
lnSuccess = 0
* Assumes Chilkat is unlocked. See Global Unlock Sample.
loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
? "Could not read gmail-mail-token.json from the private working directory."
RELEASE loToken
CANCEL
ENDIF
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
? "An OAuth access token is required. Authorize or refresh the account first."
RELEASE loToken
CANCEL
ENDIF
lcMailbox = "user@example.com"
loMailman = CreateObject('Chilkat.MailMan')
loMailman.MailHost = "pop.gmail.com"
loMailman.MailPort = 995
loMailman.PopSsl = 1
loMailman.RequireSslCertVerify = 1
loMailman.PopUsername = lcMailbox
loMailman.PopPassword = ""
loMailman.OAuth2AccessToken = loToken.StringOf("access_token")
lnSuccess = loMailman.Pop3Connect()
IF (lnSuccess <> 1) THEN
? "Could not establish the Gmail POP3 TLS connection."
RELEASE loToken
RELEASE loMailman
CANCEL
ENDIF
lnSuccess = loMailman.Pop3Authenticate()
IF (lnSuccess <> 1) THEN
loMailman.Pop3EndSessionNoQuit()
? "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy."
RELEASE loToken
RELEASE loMailman
CANCEL
ENDIF
loUidls = CreateObject('Chilkat.StringTable')
lnSuccess = loMailman.FetchUidls(loUidls)
IF (lnSuccess <> 1) THEN
loMailman.Pop3EndSessionNoQuit()
? "Could not list available POP UIDLs."
RELEASE loToken
RELEASE loMailman
RELEASE loUidls
CANCEL
ENDIF
loEmail = CreateObject('Chilkat.Email')
i = 0
DO WHILE ((i < loUidls.Count) AND (i < 10))
lcUidl = loUidls.StringAt(i)
* With headerOnly=true, zero requests headers plus the first body line.
* Chilkat uses TOP rather than RETR; this example does not issue DELE.
lnSuccess = loMailman.FetchByUidl(lcUidl,1,0,loEmail)
IF (lnSuccess <> 1) THEN
loMailman.Pop3EndSessionNoQuit()
? "Could not read message headers."
RELEASE loToken
RELEASE loMailman
RELEASE loUidls
RELEASE loEmail
CANCEL
ENDIF
? "UIDL: " + lcUidl
? "From: " + loEmail.From
? "Subject: " + loEmail.Subject
i = i + 1
ENDDO
lnSuccess = loMailman.Pop3EndSession()
IF (lnSuccess <> 1) THEN
? "Headers were read, but closing the POP session failed."
RELEASE loToken
RELEASE loMailman
RELEASE loUidls
RELEASE loEmail
CANCEL
ENDIF
? "Headers inspected: " + STR(i)
RELEASE loToken
RELEASE loMailman
RELEASE loUidls
RELEASE loEmail