Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// 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.
loMailman = createobject("CkMailMan")
// Set the POP3 server's hostname
loMailman.MailHost = "pop.example.com"
// Set the POP3 login/password.
loMailman.PopUsername = "***"
loMailman.PopPassword = "***"
// Keep a records of already-seen UIDLs in hash table serialized to an XML file.
lcSeenUidlsPath = "c:/temp/seenUidls.xml"
loSbXml = createobject("CkStringBuilder")
loHtSeenUidls = createobject("CkHashtable")
loFac = createobject("CkFileAccess")
if (loFac.FileExists(lcSeenUidlsPath) = .T.) then
llSuccess = loSbXml.LoadFile(lcSeenUidlsPath,"utf-8")
if (llSuccess = .F.) then
? loSbXml.LastErrorText
release loMailman
release loSbXml
release loHtSeenUidls
release loFac
return
endif
loHtSeenUidls.AddFromXmlSb(loSbXml)
endif
// Get the complete list of UIDLs on the mail server.
loStUidls = createobject("CkStringTable")
llSuccess = loMailman.FetchUidls(loStUidls)
if (llSuccess = .F.) then
? loMailman.LastErrorText
release loMailman
release loSbXml
release loHtSeenUidls
release loFac
release loStUidls
return
endif
// Build a list of unseen UIDLs
loStUnseenUidls = createobject("CkStringTable")
i = 0
lnCount = loStUidls.Count
do while i < lnCount
lcUidl = loStUidls.StringAt(i)
if (loHtSeenUidls.Contains(lcUidl) <> .T.) then
loStUnseenUidls.Append(lcUidl)
endif
i = i + 1
enddo
if (loStUnseenUidls.Count = 0) then
? "No unseen emails!"
release loMailman
release loSbXml
release loHtSeenUidls
release loFac
release loStUidls
release loStUnseenUidls
return
endif
// Download the unseen emails, adding each UIDL to the "seen" hash table.
loEmail = createobject("CkEmail")
lnCount = loStUnseenUidls.Count
i = 0
do while i < lnCount
// Download the full email.
lcUidl = loStUnseenUidls.StringAt(i)
llSuccess = loMailman.FetchByUidl(lcUidl,.F.,0,loEmail)
if (llSuccess = .F.) then
? loMailman.LastErrorText
release loMailman
release loSbXml
release loHtSeenUidls
release loFac
release loStUidls
release loStUnseenUidls
release loEmail
return
endif
? str(i)
? "From: " + loEmail.From
? "Subject: " + loEmail.Subject
// Add this UIDL to the "seen" hash table.
loHtSeenUidls.AddStr(lcUidl,"")
i = i + 1
enddo
loMailman.Pop3EndSession()
// Update the "seen" UIDLs file.
loSbXml.Clear()
loHtSeenUidls.ToXmlSb(loSbXml)
llSuccess = loSbXml.WriteFile(lcSeenUidlsPath,"utf-8",.F.)
if (llSuccess = .F.) then
? loSbXml.LastErrorText
release loMailman
release loSbXml
release loHtSeenUidls
release loFac
release loStUidls
release loStUnseenUidls
release loEmail
return
endif
? "Success."
release loMailman
release loSbXml
release loHtSeenUidls
release loFac
release loStUidls
release loStUnseenUidls
release loEmail