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

Visual FoxPro
LOCAL lnSuccess
LOCAL lcMailbox
LOCAL lcAppPassword
LOCAL loFiles
LOCAL loMailman
LOCAL loJoined1
LOCAL loUidls
LOCAL loHash
LOCAL loMime
LOCAL lnSaved
LOCAL lnSkipped
LOCAL i
LOCAL lcUidl
LOCAL loJoined2
LOCAL lcFileKey
LOCAL loJoined3
LOCAL lcPath
LOCAL lnExists
LOCAL loJoined4
LOCAL loJoined5

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

*  Use a private directory dedicated to this application's downloaded mail.
lcMailbox = "user@example.com"
lcAppPassword = "YOUR_16_CHARACTER_APP_PASSWORD"
loFiles = CreateObject('Chilkat.FileAccess')
lnSuccess = loFiles.DirEnsureExists("gmail-pop-mail")
IF (lnSuccess <> 1) THEN
    ? "Could not create the private download directory."
    RELEASE loFiles
    CANCEL
ENDIF

loMailman = CreateObject('Chilkat.MailMan')
loMailman.MailHost = "pop.gmail.com"
loMailman.MailPort = 995
loMailman.PopSsl = 1
loMailman.RequireSslCertVerify = 1
loJoined1 = CreateObject('Chilkat.StringBuilder')
loJoined1.Append("recent:")
loJoined1.Append(lcMailbox)
loMailman.PopUsername = loJoined1.GetAsString()
loMailman.PopPassword = lcAppPassword
*  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.
lnSuccess = loMailman.Pop3BeginSession()
IF (lnSuccess <> 1) THEN
    loMailman.Pop3EndSessionNoQuit()
    ? "POP login failed. Check the app password, POP setting, and account policy."
    RELEASE loFiles
    RELEASE loMailman
    RELEASE loJoined1
    CANCEL
ENDIF

loUidls = CreateObject('Chilkat.StringTable')
lnSuccess = loMailman.FetchUidls(loUidls)
IF (lnSuccess <> 1) THEN
    loMailman.Pop3EndSessionNoQuit()
    ? "Could not list POP UIDLs."
    RELEASE loFiles
    RELEASE loMailman
    RELEASE loJoined1
    RELEASE loUidls
    CANCEL
ENDIF

loHash = CreateObject('Chilkat.Crypt2')
loHash.HashAlgorithm = "sha256"
loHash.EncodingMode = "hex"
loHash.Charset = "utf-8"
loMime = CreateObject('Chilkat.BinData')
lnSaved = 0
lnSkipped = 0
i = 0
DO WHILE (i < loUidls.Count)
    lcUidl = loUidls.StringAt(i)
    *  Never use the opaque server UIDL as a filesystem path.
    loJoined2 = CreateObject('Chilkat.StringBuilder')
    loJoined2.Append(lcMailbox)
    loJoined2.Append(CHR(10))
    loJoined2.Append(lcUidl)
    lcFileKey = loHash.HashStringENC(loJoined2.GetAsString())
    IF (loHash.LastMethodSuccess <> 1) THEN
        loMailman.Pop3EndSessionNoQuit()
        ? "Could not construct the local UIDL key."
        RELEASE loFiles
        RELEASE loMailman
        RELEASE loJoined1
        RELEASE loUidls
        RELEASE loHash
        RELEASE loMime
        RELEASE loJoined2
        CANCEL
    ENDIF

    loJoined3 = CreateObject('Chilkat.StringBuilder')
    loJoined3.Append("gmail-pop-mail/")
    loJoined3.Append(lcFileKey)
    loJoined3.Append(".eml")
    lcPath = loJoined3.GetAsString()
    lnExists = loFiles.FileExists3(lcPath)
    IF (lnExists < 0) THEN
        loMailman.Pop3EndSessionNoQuit()
        ? "Could not check the local download receipt."
        RELEASE loFiles
        RELEASE loMailman
        RELEASE loJoined1
        RELEASE loUidls
        RELEASE loHash
        RELEASE loMime
        RELEASE loJoined2
        RELEASE loJoined3
        CANCEL
    ENDIF

    IF (lnExists = 1) THEN
        lnSkipped = lnSkipped + 1
    ELSE
        lnSuccess = loMailman.FetchMimeBd(lcUidl,loMime)
        IF (lnSuccess <> 1) THEN
            loMailman.Pop3EndSessionNoQuit()
            ? "Download failed; the UIDL was not marked complete."
            RELEASE loFiles
            RELEASE loMailman
            RELEASE loJoined1
            RELEASE loUidls
            RELEASE loHash
            RELEASE loMime
            RELEASE loJoined2
            RELEASE loJoined3
            CANCEL
        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.
        loJoined4 = CreateObject('Chilkat.StringBuilder')
        loJoined4.Append(lcPath)
        loJoined4.Append(".part")
        lnSuccess = loMime.WriteFile(loJoined4.GetAsString())
        IF (lnSuccess <> 1) THEN
            loMailman.Pop3EndSessionNoQuit()
            ? "Could not save the complete MIME file."
            RELEASE loFiles
            RELEASE loMailman
            RELEASE loJoined1
            RELEASE loUidls
            RELEASE loHash
            RELEASE loMime
            RELEASE loJoined2
            RELEASE loJoined3
            RELEASE loJoined4
            CANCEL
        ENDIF

        loJoined5 = CreateObject('Chilkat.StringBuilder')
        loJoined5.Append(lcPath)
        loJoined5.Append(".part")
        lnSuccess = loFiles.FileRename(loJoined5.GetAsString(),lcPath)
        IF (lnSuccess <> 1) THEN
            loMailman.Pop3EndSessionNoQuit()
            ? "Could not commit the downloaded message. Keep the temporary file for diagnosis."
            RELEASE loFiles
            RELEASE loMailman
            RELEASE loJoined1
            RELEASE loUidls
            RELEASE loHash
            RELEASE loMime
            RELEASE loJoined2
            RELEASE loJoined3
            RELEASE loJoined4
            RELEASE loJoined5
            CANCEL
        ENDIF

        lnSaved = lnSaved + 1
    ENDIF

    i = i + 1
ENDDO
*  No DELE commands: leave the messages on Gmail for other clients/recent-mode polls.
lnSuccess = loMailman.Pop3EndSession()
? "Saved: " + STR(lnSaved) + "  Already saved: " + STR(lnSkipped)
IF (lnSuccess <> 1) THEN
    ? "The local files are committed, but closing the POP session failed."
ENDIF

RELEASE loFiles
RELEASE loMailman
RELEASE loJoined1
RELEASE loUidls
RELEASE loHash
RELEASE loMime
RELEASE loJoined2
RELEASE loJoined3
RELEASE loJoined4
RELEASE loJoined5