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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not read gmail-mail-token.json from the private working directory.")
WScript.Quit
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
outFile.WriteLine("An OAuth access token is required. Authorize or refresh the account first.")
WScript.Quit
End If
mailbox = "user@example.com"
set mailman = 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
outFile.WriteLine("Could not establish the Gmail POP3 TLS connection.")
WScript.Quit
End If
success = mailman.Pop3Authenticate()
If (success <> 1) Then
success = mailman.Pop3EndSessionNoQuit()
outFile.WriteLine("POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.")
WScript.Quit
End If
set uidls = CreateObject("Chilkat.StringTable")
success = mailman.FetchUidls(uidls)
If (success <> 1) Then
success = mailman.Pop3EndSessionNoQuit()
outFile.WriteLine("Could not list available POP UIDLs.")
WScript.Quit
End If
set email = 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()
outFile.WriteLine("Could not read message headers.")
WScript.Quit
End If
outFile.WriteLine("UIDL: " & uidl)
outFile.WriteLine("From: " & email.From)
outFile.WriteLine("Subject: " & email.Subject)
i = i + 1
Loop
success = mailman.Pop3EndSession()
If (success <> 1) Then
outFile.WriteLine("Headers were read, but closing the POP session failed.")
WScript.Quit
End If
outFile.WriteLine("Headers inspected: " & i)
outFile.Close