Sample code for 30+ languages & platforms
PureBasic

Reading Unread POP3 Email

The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailMan.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkHashtable.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkStringTable.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; The mailman object is used for receiving (POP3) 
    ; and sending (SMTP) email.
    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Set the POP3 server's hostname
    CkMailMan::setCkMailHost(mailman, "pop.example.com")

    ; Set the POP3 login/password.
    CkMailMan::setCkPopUsername(mailman, "***")
    CkMailMan::setCkPopPassword(mailman, "***")

    ; Keep a records of already-seen UIDLs in hash table serialized to an XML file.
    seenUidlsPath.s = "c:/temp/seenUidls.xml"

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

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

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

    If CkFileAccess::ckFileExists(fac,seenUidlsPath) = 1
        success = CkStringBuilder::ckLoadFile(sbXml,seenUidlsPath,"utf-8")
        If success = 0
            Debug CkStringBuilder::ckLastErrorText(sbXml)
            CkMailMan::ckDispose(mailman)
            CkStringBuilder::ckDispose(sbXml)
            CkHashtable::ckDispose(htSeenUidls)
            CkFileAccess::ckDispose(fac)
            ProcedureReturn
        EndIf

        CkHashtable::ckAddFromXmlSb(htSeenUidls,sbXml)
    EndIf

    ; Get the complete list of UIDLs on the mail server.
    stUidls.i = CkStringTable::ckCreate()
    If stUidls.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMailMan::ckFetchUidls(mailman,stUidls)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        CkStringBuilder::ckDispose(sbXml)
        CkHashtable::ckDispose(htSeenUidls)
        CkFileAccess::ckDispose(fac)
        CkStringTable::ckDispose(stUidls)
        ProcedureReturn
    EndIf

    ; Build a list of unseen UIDLs
    stUnseenUidls.i = CkStringTable::ckCreate()
    If stUnseenUidls.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    uidl.s
    i.i = 0
    count.i = CkStringTable::ckCount(stUidls)
    While i < count
        uidl = CkStringTable::ckStringAt(stUidls,i)
        If CkHashtable::ckContains(htSeenUidls,uidl) <> 1
            CkStringTable::ckAppend(stUnseenUidls,uidl)
        EndIf

        i = i + 1
    Wend

    If CkStringTable::ckCount(stUnseenUidls) = 0
        Debug "No unseen emails!"
        CkMailMan::ckDispose(mailman)
        CkStringBuilder::ckDispose(sbXml)
        CkHashtable::ckDispose(htSeenUidls)
        CkFileAccess::ckDispose(fac)
        CkStringTable::ckDispose(stUidls)
        CkStringTable::ckDispose(stUnseenUidls)
        ProcedureReturn
    EndIf

    ; Download the unseen emails, adding each UIDL to the "seen" hash table.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    count = CkStringTable::ckCount(stUnseenUidls)
    i = 0
    While i < count
        ; Download the full email.
        uidl = CkStringTable::ckStringAt(stUnseenUidls,i)
        success = CkMailMan::ckFetchByUidl(mailman,uidl,0,0,email)
        If success = 0
            Debug CkMailMan::ckLastErrorText(mailman)
            CkMailMan::ckDispose(mailman)
            CkStringBuilder::ckDispose(sbXml)
            CkHashtable::ckDispose(htSeenUidls)
            CkFileAccess::ckDispose(fac)
            CkStringTable::ckDispose(stUidls)
            CkStringTable::ckDispose(stUnseenUidls)
            CkEmail::ckDispose(email)
            ProcedureReturn
        EndIf

        Debug Str(i)
        Debug "From: " + CkEmail::ckFrom(email)
        Debug "Subject: " + CkEmail::ckSubject(email)

        ; Add this UIDL to the "seen" hash table.
        CkHashtable::ckAddStr(htSeenUidls,uidl,"")

        i = i + 1
    Wend

    CkMailMan::ckPop3EndSession(mailman)

    ; Update the "seen" UIDLs file.
    CkStringBuilder::ckClear(sbXml)
    CkHashtable::ckToXmlSb(htSeenUidls,sbXml)
    success = CkStringBuilder::ckWriteFile(sbXml,seenUidlsPath,"utf-8",0)
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sbXml)
        CkMailMan::ckDispose(mailman)
        CkStringBuilder::ckDispose(sbXml)
        CkHashtable::ckDispose(htSeenUidls)
        CkFileAccess::ckDispose(fac)
        CkStringTable::ckDispose(stUidls)
        CkStringTable::ckDispose(stUnseenUidls)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Success."


    CkMailMan::ckDispose(mailman)
    CkStringBuilder::ckDispose(sbXml)
    CkHashtable::ckDispose(htSeenUidls)
    CkFileAccess::ckDispose(fac)
    CkStringTable::ckDispose(stUidls)
    CkStringTable::ckDispose(stUnseenUidls)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure