Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = 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.
$oMailman = ObjCreate("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.
Local $seenUidlsPath = "c:/temp/seenUidls.xml"

$oSbXml = ObjCreate("Chilkat.StringBuilder")
$oHtSeenUidls = ObjCreate("Chilkat.Hashtable")
$oFac = ObjCreate("Chilkat.FileAccess")
If ($oFac.FileExists($seenUidlsPath) = True) Then
    $bSuccess = $oSbXml.LoadFile($seenUidlsPath,"utf-8")
    If ($bSuccess = False) Then
        ConsoleWrite($oSbXml.LastErrorText & @CRLF)
        Exit
    EndIf

    $oHtSeenUidls.AddFromXmlSb($oSbXml)
EndIf

; Get the complete list of UIDLs on the mail server.
$oStUidls = ObjCreate("Chilkat.StringTable")
$bSuccess = $oMailman.FetchUidls($oStUidls)
If ($bSuccess = False) Then
    ConsoleWrite($oMailman.LastErrorText & @CRLF)
    Exit
EndIf

; Build a list of unseen UIDLs
$oStUnseenUidls = ObjCreate("Chilkat.StringTable")

Local $sUidl
Local $i = 0
Local $iCount = $oStUidls.Count
While $i < $iCount
    $sUidl = $oStUidls.StringAt($i)
    If ($oHtSeenUidls.Contains($sUidl) <> True) Then
        $oStUnseenUidls.Append($sUidl)
    EndIf

    $i = $i + 1
Wend

If ($oStUnseenUidls.Count = 0) Then
    ConsoleWrite("No unseen emails!" & @CRLF)
    Exit
EndIf

; Download the unseen emails, adding each UIDL to the "seen" hash table.
$oEmail = ObjCreate("Chilkat.Email")

$iCount = $oStUnseenUidls.Count
$i = 0
While $i < $iCount
    ; Download the full email.
    $sUidl = $oStUnseenUidls.StringAt($i)
    $bSuccess = $oMailman.FetchByUidl($sUidl,False,0,$oEmail)
    If ($bSuccess = False) Then
        ConsoleWrite($oMailman.LastErrorText & @CRLF)
        Exit
    EndIf

    ConsoleWrite($i & @CRLF)
    ConsoleWrite("From: " & $oEmail.From & @CRLF)
    ConsoleWrite("Subject: " & $oEmail.Subject & @CRLF)

    ; Add this UIDL to the "seen" hash table.
    $oHtSeenUidls.AddStr($sUidl,"")

    $i = $i + 1
Wend

$oMailman.Pop3EndSession()

; Update the "seen" UIDLs file.
$oSbXml.Clear 
$oHtSeenUidls.ToXmlSb($oSbXml)
$bSuccess = $oSbXml.WriteFile($seenUidlsPath,"utf-8",False)
If ($bSuccess = False) Then
    ConsoleWrite($oSbXml.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Success." & @CRLF)