Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.0+

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.

Reference documentation

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-mail-token.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read gmail-mail-token.json from the private working directory." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("An OAuth access token is required. Authorize or refresh the account first." & @CRLF)
    Exit
EndIf

Local $sMailbox = "user@example.com"
$oMailman = ObjCreate("Chilkat.MailMan")
$oMailman.MailHost = "pop.gmail.com"
$oMailman.MailPort = 995
$oMailman.PopSsl = True
$oMailman.RequireSslCertVerify = True
$oMailman.PopUsername = $sMailbox
$oMailman.PopPassword = ""
$oMailman.OAuth2AccessToken = $oToken.StringOf("access_token")
$bSuccess = $oMailman.Pop3Connect()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not establish the Gmail POP3 TLS connection." & @CRLF)
    Exit
EndIf

$bSuccess = $oMailman.Pop3Authenticate()
If ($bSuccess <> True) Then
    $oMailman.Pop3EndSessionNoQuit()
    ConsoleWrite("POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy." & @CRLF)
    Exit
EndIf

$oUidls = ObjCreate("Chilkat.StringTable")
$bSuccess = $oMailman.FetchUidls($oUidls)
If ($bSuccess <> True) Then
    $oMailman.Pop3EndSessionNoQuit()
    ConsoleWrite("Could not list available POP UIDLs." & @CRLF)
    Exit
EndIf

$oEmail = ObjCreate("Chilkat.Email")
Local $i = 0
While (($i < $oUidls.Count) And ($i < 10))
Local $sUidl = $oUidls.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.
    $bSuccess = $oMailman.FetchByUidl($sUidl,True,0,$oEmail)
    If ($bSuccess <> True) Then
        $oMailman.Pop3EndSessionNoQuit()
        ConsoleWrite("Could not read message headers." & @CRLF)
        Exit
    EndIf

    ConsoleWrite("UIDL: " & $sUidl & @CRLF)
    ConsoleWrite("From: " & $oEmail.From & @CRLF)
    ConsoleWrite("Subject: " & $oEmail.Subject & @CRLF)
    $i = $i + 1
Wend
$bSuccess = $oMailman.Pop3EndSession()
If ($bSuccess <> True) Then
    ConsoleWrite("Headers were read, but closing the POP session failed." & @CRLF)
    Exit
EndIf

ConsoleWrite("Headers inspected: " & $i & @CRLF)