Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

POP3: Require an STLS Upgrade Before Authentication

Connect to a provider that supports POP STLS on port 110, require the TLS upgrade, then authenticate and obtain a message count. Set PopSsl=false, Pop3Stls=true, and enable certificate verification. Replace the generic host and credentials with settings issued by that provider. Use the provider's OAuth or app-password policy where applicable.

The former example was mislabeled: it connected to Gmail on port 995 using implicit TLS and never performed STLS. Gmail's documented POP configuration remains pop.gmail.com:995 with PopSsl=true; use Gmail POP OAuth2 for that connection.

The required STLS setting fails if the upgrade is unavailable. An opportunistic setting can leave a connection unencrypted and is not used here.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    ;  Use a provider that explicitly supports POP STLS on port 110.
    ;  Gmail documents implicit TLS on pop.gmail.com:995 instead.
    CkMailMan::setCkMailHost(mailman, "pop.example.com")
    CkMailMan::setCkMailPort(mailman, 110)
    CkMailMan::setCkPopSsl(mailman, 0)
    CkMailMan::setCkPop3Stls(mailman, 1)
    CkMailMan::setCkPop3StlsIfPossible(mailman, 0)
    CkMailMan::setCkRequireSslCertVerify(mailman, 1)
    CkMailMan::setCkPopUsername(mailman, "user@example.com")
    CkMailMan::setCkPopPassword(mailman, "YOUR_PROVIDER_ISSUED_PASSWORD")
    success = CkMailMan::ckPop3BeginSession(mailman)
    If success <> 1
        CkMailMan::ckPop3EndSessionNoQuit(mailman)
        Debug "POP connection or authentication failed. STLS must succeed before credentials are sent."
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    count.i = CkMailMan::ckCheckMail(mailman)
    If count < 0
        CkMailMan::ckPop3EndSessionNoQuit(mailman)
        Debug "Could not obtain the POP message count."
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    Debug "Available POP messages: " + Str(count)
    success = CkMailMan::ckPop3EndSession(mailman)
    If success <> 1
        Debug "The count was obtained, but closing the POP session failed."
    EndIf



    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure