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

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL cSeenUidlsPath
LOCAL oSbXml
LOCAL oHtSeenUidls
LOCAL oFac
LOCAL oStUidls
LOCAL oStUnseenUidls
LOCAL cUidl
LOCAL i
LOCAL nCount
LOCAL oEmail

nSuccess := 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.
oMailman := CreateObject("Chilkat.MailMan")

//  Set the POP3 server's hostname
oMailman:MailHost := "pop.example.com"

//  Set the POP3 login/password.
oMailman:PopUsername := "***"
oMailman:PopPassword := "***"

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

oSbXml := CreateObject("Chilkat.StringBuilder")
oHtSeenUidls := CreateObject("Chilkat.Hashtable")
oFac := CreateObject("Chilkat.FileAccess")
IF (oFac:FileExists(cSeenUidlsPath) == 1)
    nSuccess := oSbXml:LoadFile(cSeenUidlsPath, "utf-8")
    IF (nSuccess == 0)
        ? oSbXml:LastErrorText
        oMailman:destroy()
        oSbXml:destroy()
        oHtSeenUidls:destroy()
        oFac:destroy()
        RETURN
    ENDIF

    oHtSeenUidls:AddFromXmlSb(oSbXml)
ENDIF

//  Get the complete list of UIDLs on the mail server.
oStUidls := CreateObject("Chilkat.StringTable")
nSuccess := oMailman:FetchUidls(oStUidls)
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oSbXml:destroy()
    oHtSeenUidls:destroy()
    oFac:destroy()
    oStUidls:destroy()
    RETURN
ENDIF

//  Build a list of unseen UIDLs
oStUnseenUidls := CreateObject("Chilkat.StringTable")

i := 0
nCount := oStUidls:Count
DO WHILE i < nCount
    cUidl := oStUidls:StringAt(i)
    IF (oHtSeenUidls:Contains(cUidl) != 1)
        oStUnseenUidls:Append(cUidl)
    ENDIF

    i := i + 1
ENDDO

IF (oStUnseenUidls:Count == 0)
    ? "No unseen emails!"
    oMailman:destroy()
    oSbXml:destroy()
    oHtSeenUidls:destroy()
    oFac:destroy()
    oStUidls:destroy()
    oStUnseenUidls:destroy()
    RETURN
ENDIF

//  Download the unseen emails, adding each UIDL to the "seen" hash table.
oEmail := CreateObject("Chilkat.Email")

nCount := oStUnseenUidls:Count
i := 0
DO WHILE i < nCount
    //  Download the full email.
    cUidl := oStUnseenUidls:StringAt(i)
    nSuccess := oMailman:FetchByUidl(cUidl, 0, 0, oEmail)
    IF (nSuccess == 0)
        ? oMailman:LastErrorText
        oMailman:destroy()
        oSbXml:destroy()
        oHtSeenUidls:destroy()
        oFac:destroy()
        oStUidls:destroy()
        oStUnseenUidls:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    ? Str(i)
    ? "From: " + oEmail:From
    ? "Subject: " + oEmail:Subject

    //  Add this UIDL to the "seen" hash table.
    oHtSeenUidls:AddStr(cUidl, "")

    i := i + 1
ENDDO

oMailman:Pop3EndSession()

//  Update the "seen" UIDLs file.
oSbXml:Clear()
oHtSeenUidls:ToXmlSb(oSbXml)
nSuccess := oSbXml:WriteFile(cSeenUidlsPath, "utf-8", 0)
IF (nSuccess == 0)
    ? oSbXml:LastErrorText
    oMailman:destroy()
    oSbXml:destroy()
    oHtSeenUidls:destroy()
    oFac:destroy()
    oStUidls:destroy()
    oStUnseenUidls:destroy()
    oEmail:destroy()
    RETURN
ENDIF

? "Success."

oMailman:destroy()
oSbXml:destroy()
oHtSeenUidls:destroy()
oFac:destroy()
oStUidls:destroy()
oStUnseenUidls:destroy()
oEmail:destroy()