Sample code for 30+ languages & platforms
AutoIt

Download Unread Email from IMAP Mailbox

Download unread email messages from an IMAP mailbox.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oImap = ObjCreate("Chilkat.Imap")

; Connect to an IMAP server.
; Use TLS
$oImap.Ssl = True
$oImap.Port = 993
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Login
$bSuccess = $oImap.Login("admin@chilkatsoft.com","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Select an IMAP mailbox
$bSuccess = $oImap.SelectMailbox("Inbox")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Find emails marked as seen or not already seen:
Local $seenSearch = "SEEN"
Local $sNotSeenSearch = "NOT SEEN"

; Get the set of unseen message UIDs
Local $bFetchUids = True
$oMessageSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx($sNotSeenSearch,$bFetchUids,$oMessageSet)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Fetch the unseen emails into a bundle object:
$oBundle = ObjCreate("Chilkat.EmailBundle")
Local $bHeadersOnly = False
$bSuccess = $oImap.FetchMsgSet($bHeadersOnly,$oMessageSet,$oBundle)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

; Display the Subject and From of each email.
$oEmail = ObjCreate("Chilkat.Email")
Local $i = 0
Local $iNumEmails = $oBundle.MessageCount
While $i < $iNumEmails
    $oBundle.EmailAt($i,$oEmail)

    ConsoleWrite($oEmail.GetHeaderField("Date") & @CRLF)
    ConsoleWrite($oEmail.Subject & @CRLF)
    ConsoleWrite($oEmail.From & @CRLF)
    ConsoleWrite("--" & @CRLF)
    $i = $i + 1
Wend

; Disconnect from the IMAP server.
$bSuccess = $oImap.Disconnect()