Sample code for 30+ languages & platforms
VBScript 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 VBScript Downloads

VBScript
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.

'  Use a private directory dedicated to this application's downloaded mail.
mailbox = "user@example.com"
appPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
set files = CreateObject("Chilkat.FileAccess")
success = files.DirEnsureExists("gmail-pop-mail")
If (success <> 1) Then
    outFile.WriteLine("Could not create the private download directory.")
    WScript.Quit
End If

set mailman = CreateObject("Chilkat.MailMan")
mailman.MailHost = "pop.gmail.com"
mailman.MailPort = 995
mailman.PopSsl = 1
mailman.RequireSslCertVerify = 1
set joined1 = CreateObject("Chilkat.StringBuilder")
success = joined1.Append("recent:")
success = joined1.Append(mailbox)
mailman.PopUsername = joined1.GetAsString()
mailman.PopPassword = appPassword
'  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.
success = mailman.Pop3BeginSession()
If (success <> 1) Then
    success = mailman.Pop3EndSessionNoQuit()
    outFile.WriteLine("POP login failed. Check the app password, POP setting, and account 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 POP UIDLs.")
    WScript.Quit
End If

set hash = CreateObject("Chilkat.Crypt2")
hash.HashAlgorithm = "sha256"
hash.EncodingMode = "hex"
hash.Charset = "utf-8"
set mime = CreateObject("Chilkat.BinData")
saved = 0
skipped = 0
i = 0
Do While (i < uidls.Count)
    uidl = uidls.StringAt(i)
    '  Never use the opaque server UIDL as a filesystem path.
    set joined2 = CreateObject("Chilkat.StringBuilder")
    success = joined2.Append(mailbox)
    success = joined2.Append(vbLf)
    success = joined2.Append(uidl)
    fileKey = hash.HashStringENC(joined2.GetAsString())
    If (hash.LastMethodSuccess <> 1) Then
        success = mailman.Pop3EndSessionNoQuit()
        outFile.WriteLine("Could not construct the local UIDL key.")
        WScript.Quit
    End If

    set joined3 = CreateObject("Chilkat.StringBuilder")
    success = joined3.Append("gmail-pop-mail/")
    success = joined3.Append(fileKey)
    success = joined3.Append(".eml")
    path = joined3.GetAsString()
    exists = files.FileExists3(path)
    If (exists < 0) Then
        success = mailman.Pop3EndSessionNoQuit()
        outFile.WriteLine("Could not check the local download receipt.")
        WScript.Quit
    End If

    If (exists = 1) Then
        skipped = skipped + 1
    Else
        success = mailman.FetchMimeBd(uidl,mime)
        If (success <> 1) Then
            success = mailman.Pop3EndSessionNoQuit()
            outFile.WriteLine("Download failed; the UIDL was not marked complete.")
            WScript.Quit
        End If

        '  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.
        set joined4 = CreateObject("Chilkat.StringBuilder")
        success = joined4.Append(path)
        success = joined4.Append(".part")
        success = mime.WriteFile(joined4.GetAsString())
        If (success <> 1) Then
            success = mailman.Pop3EndSessionNoQuit()
            outFile.WriteLine("Could not save the complete MIME file.")
            WScript.Quit
        End If

        set joined5 = CreateObject("Chilkat.StringBuilder")
        success = joined5.Append(path)
        success = joined5.Append(".part")
        success = files.FileRename(joined5.GetAsString(),path)
        If (success <> 1) Then
            success = mailman.Pop3EndSessionNoQuit()
            outFile.WriteLine("Could not commit the downloaded message. Keep the temporary file for diagnosis.")
            WScript.Quit
        End If

        saved = saved + 1
    End If

    i = i + 1
Loop
'  No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
success = mailman.Pop3EndSession()
outFile.WriteLine("Saved: " & saved & "  Already saved: " & skipped)
If (success <> 1) Then
    outFile.WriteLine("The local files are committed, but closing the POP session failed.")
End If


outFile.Close