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

PureBasic
IncludeFile "CkStringTable.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token.i = CkJsonObject::ckCreate()
    If token.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJsonObject::ckLoadFile(token,"gmail-mail-token.json")
    If success <> 1
        Debug "Could not read gmail-mail-token.json from the private working directory."
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
        Debug "An OAuth access token is required. Authorize or refresh the account first."
        CkJsonObject::ckDispose(token)
        ProcedureReturn
    EndIf

    mailbox.s = "user@example.com"
    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkMailMan::setCkMailHost(mailman, "pop.gmail.com")
    CkMailMan::setCkMailPort(mailman, 995)
    CkMailMan::setCkPopSsl(mailman, 1)
    CkMailMan::setCkRequireSslCertVerify(mailman, 1)
    CkMailMan::setCkPopUsername(mailman, mailbox)
    CkMailMan::setCkPopPassword(mailman, "")
    CkMailMan::setCkOAuth2AccessToken(mailman, CkJsonObject::ckStringOf(token,"access_token"))
    success = CkMailMan::ckPop3Connect(mailman)
    If success <> 1
        Debug "Could not establish the Gmail POP3 TLS connection."
        CkJsonObject::ckDispose(token)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    success = CkMailMan::ckPop3Authenticate(mailman)
    If success <> 1
        CkMailMan::ckPop3EndSessionNoQuit(mailman)
        Debug "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy."
        CkJsonObject::ckDispose(token)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    uidls.i = CkStringTable::ckCreate()
    If uidls.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMailMan::ckFetchUidls(mailman,uidls)
    If success <> 1
        CkMailMan::ckPop3EndSessionNoQuit(mailman)
        Debug "Could not list available POP UIDLs."
        CkJsonObject::ckDispose(token)
        CkMailMan::ckDispose(mailman)
        CkStringTable::ckDispose(uidls)
        ProcedureReturn
    EndIf

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    While ((i < CkStringTable::ckCount(uidls)) AND (i < 10))
        uidl.s = CkStringTable::ckStringAt(uidls,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 = CkMailMan::ckFetchByUidl(mailman,uidl,1,0,email)
        If success <> 1
            CkMailMan::ckPop3EndSessionNoQuit(mailman)
            Debug "Could not read message headers."
            CkJsonObject::ckDispose(token)
            CkMailMan::ckDispose(mailman)
            CkStringTable::ckDispose(uidls)
            CkEmail::ckDispose(email)
            ProcedureReturn
        EndIf

        Debug "UIDL: " + uidl
        Debug "From: " + CkEmail::ckFrom(email)
        Debug "Subject: " + CkEmail::ckSubject(email)
        i = i + 1
    Wend
    success = CkMailMan::ckPop3EndSession(mailman)
    If success <> 1
        Debug "Headers were read, but closing the POP session failed."
        CkJsonObject::ckDispose(token)
        CkMailMan::ckDispose(mailman)
        CkStringTable::ckDispose(uidls)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Headers inspected: " + Str(i)


    CkJsonObject::ckDispose(token)
    CkMailMan::ckDispose(mailman)
    CkStringTable::ckDispose(uidls)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure