Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.0+

Sorting Email

Demonstrates how to sort an email bundle.

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 the 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

Local $bFetchUids = True
$oMessageSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("ALL",$bFetchUids,$oMessageSet)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

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

;  Sort the email bundle by date, recipient, sender, or subject:
Local $bAscending = True
$oBundle.SortByDate $bAscending

;  To sort by recipient, sender, or subject, call 
;  SortBySender, SortByRecipient, or SortBySubject.

;  Display the Subject and From of each email.
$oEmail = ObjCreate("Chilkat.Email")
Local $i = 0
While $i < $oBundle.MessageCount
    $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()