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

Gmail POP3: Recent Mode with Persistent UIDL Tracking

See more GMail SMTP/IMAP/POP Examples

Use recent:user@example.com to access Gmail's recent POP delivery window, then download each UIDL only once. This example uses an app password, requiring 2-Step Verification and an account policy that allows app passwords. Enable POP and select Keep Gmail's copy in the Inbox. An ordinary account password will not work. If app passwords are unavailable, use the OAuth IMAP examples.

Recent mode exposes approximately the last 30 days of mail and is useful when multiple POP clients need the same messages. Keep the recent prefix out of SMTP settings. This is a rolling window, not a complete-mailbox backup; polling after a long gap can miss older mail.

UIDLs stay as opaque strings. A SHA-256 filename derived from mailbox plus UIDL identifies each saved MIME file. The final file doubles as the persistent completion receipt: a second run skips it. A partial download or failed write is never marked complete, and a leftover .part file is retried. The code sends no DELE commands.

Keep this private directory dedicated to the downloader, use a stable mailbox spelling, and run only one writer at a time. Moving or deleting final files discards the tracking information and permits a new download while the message is still available. For business processing beyond saving MIME, use an application transaction and idempotent handling.

Google documentation

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

;  Use a private directory dedicated to this application's downloaded mail.
Local $sMailbox = "user@example.com"
Local $sAppPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
$oFiles = ObjCreate("Chilkat.FileAccess")
$bSuccess = $oFiles.DirEnsureExists("gmail-pop-mail")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not create the private download directory." & @CRLF)
    Exit
EndIf

$oMailman = ObjCreate("Chilkat.MailMan")
$oMailman.MailHost = "pop.gmail.com"
$oMailman.MailPort = 995
$oMailman.PopSsl = True
$oMailman.RequireSslCertVerify = True
$oJoined1 = ObjCreate("Chilkat.StringBuilder")
$oJoined1.Append("recent:")
$oJoined1.Append($sMailbox)
$oMailman.PopUsername = $oJoined1.GetAsString()
$oMailman.PopPassword = $sAppPassword
;  Use an eligible account's app password loaded from protected configuration.
;  The recent: prefix is only for POP; never add it to an SMTP username.
$bSuccess = $oMailman.Pop3BeginSession()
If ($bSuccess <> True) Then
    $oMailman.Pop3EndSessionNoQuit()
    ConsoleWrite("POP login failed. Check the app password, POP setting, and account policy." & @CRLF)
    Exit
EndIf

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

$oHash = ObjCreate("Chilkat.Crypt2")
$oHash.HashAlgorithm = "sha256"
$oHash.EncodingMode = "hex"
$oHash.Charset = "utf-8"
$oMime = ObjCreate("Chilkat.BinData")
Local $iSaved = 0
Local $iSkipped = 0
Local $i = 0
While ($i < $oUidls.Count)
Local $sUidl = $oUidls.StringAt($i)
    ;  Never use the opaque server UIDL as a filesystem path.
    $oJoined2 = ObjCreate("Chilkat.StringBuilder")
    $oJoined2.Append($sMailbox)
    $oJoined2.Append(@LF)
    $oJoined2.Append($sUidl)
Local $sFileKey = $oHash.HashStringENC($oJoined2.GetAsString())
    If ($oHash.LastMethodSuccess <> True) Then
        $oMailman.Pop3EndSessionNoQuit()
        ConsoleWrite("Could not construct the local UIDL key." & @CRLF)
        Exit
    EndIf

    $oJoined3 = ObjCreate("Chilkat.StringBuilder")
    $oJoined3.Append("gmail-pop-mail/")
    $oJoined3.Append($sFileKey)
    $oJoined3.Append(".eml")
Local $sPath = $oJoined3.GetAsString()
Local $iExists = $oFiles.FileExists3($sPath)
    If ($iExists < 0) Then
        $oMailman.Pop3EndSessionNoQuit()
        ConsoleWrite("Could not check the local download receipt." & @CRLF)
        Exit
    EndIf

    If ($iExists = 1) Then
        $iSkipped = $iSkipped + 1
    Else
        $bSuccess = $oMailman.FetchMimeBd($sUidl,$oMime)
        If ($bSuccess <> True) Then
            $oMailman.Pop3EndSessionNoQuit()
            ConsoleWrite("Download failed; the UIDL was not marked complete." & @CRLF)
            Exit
        EndIf

        ;  A final .eml file is the persistent completion receipt for this UIDL.
        ;  Save to a sibling temporary file first. A leftover .part file is not a receipt.
        $oJoined4 = ObjCreate("Chilkat.StringBuilder")
        $oJoined4.Append($sPath)
        $oJoined4.Append(".part")
        $bSuccess = $oMime.WriteFile($oJoined4.GetAsString())
        If ($bSuccess <> True) Then
            $oMailman.Pop3EndSessionNoQuit()
            ConsoleWrite("Could not save the complete MIME file." & @CRLF)
            Exit
        EndIf

        $oJoined5 = ObjCreate("Chilkat.StringBuilder")
        $oJoined5.Append($sPath)
        $oJoined5.Append(".part")
        $bSuccess = $oFiles.FileRename($oJoined5.GetAsString(),$sPath)
        If ($bSuccess <> True) Then
            $oMailman.Pop3EndSessionNoQuit()
            ConsoleWrite("Could not commit the downloaded message. Keep the temporary file for diagnosis." & @CRLF)
            Exit
        EndIf

        $iSaved = $iSaved + 1
    EndIf

    $i = $i + 1
Wend
;  No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
$bSuccess = $oMailman.Pop3EndSession()
ConsoleWrite("Saved: " & $iSaved & "  Already saved: " & $iSkipped & @CRLF)
If ($bSuccess <> True) Then
    ConsoleWrite("The local files are committed, but closing the POP session failed." & @CRLF)
EndIf