Sample code for 30+ languages & platforms
AutoIt

Fetch Inbox Email Headers

Downloads the headers of all emails in the Inbox and shows some information about each, such as From, Subject, and whether the email has been seen (already read) or not.

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.

$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("myLogin","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

; Normally, when an email is downloaded, its "Seen" flag is automatically set.
; We don't want our program to be interfering with the "Seen" flags.
; To do this, set the PeekMode to True.
$oImap.PeekMode = True

; After selecting the mailbox, the NumMessages property
; will contain the number of emails in the mailbox.
; When sequence numbers (not UIDs) are used to reference emails,
; they range from 1 to N, where N is the number of messages in the mailbox.
; This example will download the headers by sequence numbers.
Local $iCount = $oImap.NumMessages

$oBundle = ObjCreate("Chilkat.EmailBundle")
$bSuccess = $oImap.FetchRange(True,1,$iCount,$oBundle)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

If ($oBundle.MessageCount = 0) Then
    ConsoleWrite("No messages in Inbox." & @CRLF)
    Exit
EndIf

; Loop over the email headers.  
$oEmailHeader = ObjCreate("Chilkat.Email")
Local $i = 0
While $i < $oBundle.MessageCount

    $oBundle.EmailAt($i,$oEmailHeader)

    ConsoleWrite($oEmailHeader.From & @CRLF)
    ConsoleWrite($oEmailHeader.Subject & @CRLF)

    ; Get the "Seen" flag.
    ; When an email is fetched from the IMAP server, the email flags are stored
    ; within the email in the "ckx-imap-flags" header.
    ; The call to GetMailFlag is simply getting the information from the email header.
    ; (it is not communicating with the IMAP server to get this information, because
    ; the information was obtained when downloading the headers)
Local $iSeenFlag = $oImap.GetMailFlag($oEmailHeader,"Seen")
    If ($iSeenFlag = 1) Then
        ConsoleWrite("This email has been read (already seen)" & @CRLF)
    Else
        ConsoleWrite("This email has not yet been read (it is not yet seen)" & @CRLF)
    EndIf

    ; We can also look directly at the "ckx-imap-flags" header:
    ConsoleWrite("ckx-imap-flags: " & $oEmailHeader.GetHeaderField("ckx-imap-flags") & @CRLF)

    ConsoleWrite("--" & @CRLF)

    $i = $i + 1
Wend

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