Xojo Plugin
Xojo Plugin
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 Xojo Plugin Downloads
Dim success As Boolean
success = False
// 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.
Dim mailman As New Chilkat.MailMan
// Set the POP3 server's hostname
mailman.MailHost = "pop.example.com"
// Set the POP3 login/password.
mailman.PopUsername = "***"
mailman.PopPassword = "***"
// Keep a records of already-seen UIDLs in hash table serialized to an XML file.
Dim seenUidlsPath As String
seenUidlsPath = "c:/temp/seenUidls.xml"
Dim sbXml As New Chilkat.StringBuilder
Dim htSeenUidls As New Chilkat.Hashtable
Dim fac As New Chilkat.FileAccess
If (fac.FileExists(seenUidlsPath) = True) Then
success = sbXml.LoadFile(seenUidlsPath,"utf-8")
If (success = False) Then
System.DebugLog(sbXml.LastErrorText)
Return
End If
success = htSeenUidls.AddFromXmlSb(sbXml)
End If
// Get the complete list of UIDLs on the mail server.
Dim stUidls As New Chilkat.StringTable
success = mailman.FetchUidls(stUidls)
If (success = False) Then
System.DebugLog(mailman.LastErrorText)
Return
End If
// Build a list of unseen UIDLs
Dim stUnseenUidls As New Chilkat.StringTable
Dim uidl As String
Dim i As Int32
i = 0
Dim count As Int32
count = stUidls.Count
While i < count
uidl = stUidls.StringAt(i)
If (htSeenUidls.Contains(uidl) <> True) Then
success = stUnseenUidls.Append(uidl)
End If
i = i + 1
Wend
If (stUnseenUidls.Count = 0) Then
System.DebugLog("No unseen emails!")
Return
End If
// Download the unseen emails, adding each UIDL to the "seen" hash table.
Dim email As New Chilkat.Email
count = stUnseenUidls.Count
i = 0
While i < count
// Download the full email.
uidl = stUnseenUidls.StringAt(i)
success = mailman.FetchByUidl(uidl,False,0,email)
If (success = False) Then
System.DebugLog(mailman.LastErrorText)
Return
End If
System.DebugLog(Str(i))
System.DebugLog("From: " + email.From)
System.DebugLog("Subject: " + email.Subject)
// Add this UIDL to the "seen" hash table.
success = htSeenUidls.AddStr(uidl,"")
i = i + 1
Wend
success = mailman.Pop3EndSession()
// Update the "seen" UIDLs file.
sbXml.Clear
success = htSeenUidls.ToXmlSb(sbXml)
success = sbXml.WriteFile(seenUidlsPath,"utf-8",False)
If (success = False) Then
System.DebugLog(sbXml.LastErrorText)
Return
End If
System.DebugLog("Success.")