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 Xojo Plugin Downloads
Dim success As Boolean
success = False
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-mail-token.json")
If (success <> True) Then
System.DebugLog("Could not read gmail-mail-token.json from the private working directory.")
Return
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
System.DebugLog("An OAuth access token is required. Authorize or refresh the account first.")
Return
End If
Dim mailbox As String
mailbox = "user@example.com"
Dim mailman As New Chilkat.MailMan
mailman.MailHost = "pop.gmail.com"
mailman.MailPort = 995
mailman.PopSsl = True
mailman.RequireSslCertVerify = True
mailman.PopUsername = mailbox
mailman.PopPassword = ""
mailman.OAuth2AccessToken = token.StringOf("access_token")
success = mailman.Pop3Connect()
If (success <> True) Then
System.DebugLog("Could not establish the Gmail POP3 TLS connection.")
Return
End If
success = mailman.Pop3Authenticate()
If (success <> True) Then
success = mailman.Pop3EndSessionNoQuit()
System.DebugLog("POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.")
Return
End If
Dim uidls As New Chilkat.StringTable
success = mailman.FetchUidls(uidls)
If (success <> True) Then
success = mailman.Pop3EndSessionNoQuit()
System.DebugLog("Could not list available POP UIDLs.")
Return
End If
Dim email As New Chilkat.Email
Dim i As Int32
i = 0
While ((i < uidls.Count) And (i < 10))
Dim uidl As String
uidl = uidls.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.
success = mailman.FetchByUidl(uidl,True,0,email)
If (success <> True) Then
success = mailman.Pop3EndSessionNoQuit()
System.DebugLog("Could not read message headers.")
Return
End If
System.DebugLog("UIDL: " + uidl)
System.DebugLog("From: " + email.From)
System.DebugLog("Subject: " + email.Subject)
i = i + 1
Wend
success = mailman.Pop3EndSession()
If (success <> True) Then
System.DebugLog("Headers were read, but closing the POP session failed.")
Return
End If
System.DebugLog("Headers inspected: " + Str(i))