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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    String sMailbox
    Handle hoMailman
    Variant vUidls
    Handle hoUidls
    Variant vEmail
    Handle hoEmail
    Integer i
    String sUidl
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Get ComLoadFile Of hoToken "gmail-mail-token.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read gmail-mail-token.json from the private working directory."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
    Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "An OAuth access token is required. Authorize or refresh the account first."
        Procedure_Return
    End

    Move "user@example.com" To sMailbox
    Get Create (RefClass(cComChilkatMailMan)) To hoMailman
    If (Not(IsComObjectCreated(hoMailman))) Begin
        Send CreateComObject of hoMailman
    End
    Set ComMailHost Of hoMailman To "pop.gmail.com"
    Set ComMailPort Of hoMailman To 995
    Set ComPopSsl Of hoMailman To True
    Set ComRequireSslCertVerify Of hoMailman To True
    Set ComPopUsername Of hoMailman To sMailbox
    Set ComPopPassword Of hoMailman To ""
    Get ComStringOf Of hoToken "access_token" To sTemp1
    Set ComOAuth2AccessToken Of hoMailman To sTemp1
    Get ComPop3Connect Of hoMailman To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not establish the Gmail POP3 TLS connection."
        Procedure_Return
    End

    Get ComPop3Authenticate Of hoMailman To iSuccess
    If (iSuccess <> True) Begin
        Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
        Showln "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringTable)) To hoUidls
    If (Not(IsComObjectCreated(hoUidls))) Begin
        Send CreateComObject of hoUidls
    End
    Get pvComObject of hoUidls to vUidls
    Get ComFetchUidls Of hoMailman vUidls To iSuccess
    If (iSuccess <> True) Begin
        Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
        Showln "Could not list available POP UIDLs."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Move 0 To i
    While ((i < (ComCount(hoUidls))) And (i < 10))
        Get ComStringAt Of hoUidls i To sUidl
        //  With headerOnly=true, zero requests headers plus the first body line.
        //  Chilkat uses TOP rather than RETR; this example does not issue DELE.
        Get pvComObject of hoEmail to vEmail
        Get ComFetchByUidl Of hoMailman sUidl True 0 vEmail To iSuccess
        If (iSuccess <> True) Begin
            Get ComPop3EndSessionNoQuit Of hoMailman To iSuccess
            Showln "Could not read message headers."
            Procedure_Return
        End

        Showln "UIDL: " sUidl
        Get ComFrom Of hoEmail To sTemp1
        Showln "From: " sTemp1
        Get ComSubject Of hoEmail To sTemp1
        Showln "Subject: " sTemp1
        Move (i + 1) To i
    Loop

    Get ComPop3EndSession Of hoMailman To iSuccess
    If (iSuccess <> True) Begin
        Showln "Headers were read, but closing the POP session failed."
        Procedure_Return
    End

    Showln "Headers inspected: " i


End_Procedure