Sample code for 30+ languages & platforms
AutoIt

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

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("****","****")
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

; Get the message IDs of all the emails in the mailbox
; We can choose to fetch UIDs or sequence numbers.
Local $bFetchUids = True
$oMessageSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("ALL",$bFetchUids,$oMessageSet)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

Local $iNumFound = $oMessageSet.Count
If ($iNumFound = 0) Then
    ConsoleWrite("No messages found." & @CRLF)
    Exit
EndIf

; Get the 1st 10 messages in messageSet.
Local $iUpperBound = 10
If ($iNumFound < $iUpperBound) Then
    $iUpperBound = $iNumFound
EndIf

Local $i = 0
Local $bUid = $oMessageSet.HasUids
Local $bHeaderOnly = True
$oEmail = ObjCreate("Chilkat.Email")

While $i < $iUpperBound

    $bSuccess = $oImap.FetchEmail($bHeaderOnly,$oMessageSet.GetId($i),$bUid,$oEmail)
    If ($bSuccess = False) Then
        ConsoleWrite($oImap.LastErrorText & @CRLF)
        Exit
    EndIf

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

    $i = $i + 1
Wend

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