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 Classic ASP Downloads
<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.
set token = Server.CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read gmail-mail-token.json from the private working directory.") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "An OAuth access token is required. Authorize or refresh the account first.") & "</pre>"
Response.End
End If
mailbox = "user@example.com"
set mailman = Server.CreateObject("Chilkat.MailMan")
mailman.MailHost = "pop.gmail.com"
mailman.MailPort = 995
mailman.PopSsl = 1
mailman.RequireSslCertVerify = 1
mailman.PopUsername = mailbox
mailman.PopPassword = ""
mailman.OAuth2AccessToken = token.StringOf("access_token")
success = mailman.Pop3Connect()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Gmail POP3 TLS connection.") & "</pre>"
Response.End
End If
success = mailman.Pop3Authenticate()
If (success <> 1) Then
success = mailman.Pop3EndSessionNoQuit()
Response.Write "<pre>" & Server.HTMLEncode( "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.") & "</pre>"
Response.End
End If
set uidls = Server.CreateObject("Chilkat.StringTable")
success = mailman.FetchUidls(uidls)
If (success <> 1) Then
success = mailman.Pop3EndSessionNoQuit()
Response.Write "<pre>" & Server.HTMLEncode( "Could not list available POP UIDLs.") & "</pre>"
Response.End
End If
set email = Server.CreateObject("Chilkat.Email")
i = 0
Do While ((i < uidls.Count) And (i < 10))
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,1,0,email)
If (success <> 1) Then
success = mailman.Pop3EndSessionNoQuit()
Response.Write "<pre>" & Server.HTMLEncode( "Could not read message headers.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "UIDL: " & uidl) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "From: " & email.From) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Subject: " & email.Subject) & "</pre>"
i = i + 1
Loop
success = mailman.Pop3EndSession()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Headers were read, but closing the POP session failed.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Headers inspected: " & i) & "</pre>"
%>
</body>
</html>