AutoIt
AutoIt
Read IMAP Email Headers
This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.
The example shows how to:
- Connect to an IMAP server using SSL/TLS
- Authenticate and select a mailbox
- Query for all messages in the mailbox
- Fetch header-only email information
- Display sender, subject, recipients, and total message size
- Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments
This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.
Chilkat AutoIt Downloads
Local $bSuccess = False
$bSuccess = False
; This example assumes the Chilkat API has already been unlocked.
; See Global Unlock Sample for example code.
$oImap = ObjCreate("Chilkat.Imap")
; Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
$oImap.Ssl = True
$oImap.Port = 993
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
; Authenticate with the IMAP server.
$bSuccess = $oImap.Login("****","****")
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
; Select the mailbox (folder) to access.
$bSuccess = $oImap.SelectMailbox("Inbox")
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
; Get the identifiers for all messages in the selected mailbox.
; Setting fetchUids = cktrue causes IMAP UIDs to be returned.
; If ckfalse, IMAP sequence numbers are returned instead.
Local $bFetchUids = True
$oMessageSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("ALL",$bFetchUids,$oMessageSet)
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
; Download only the email headers for the messages in the set.
; This is much faster than downloading full emails because the
; message bodies and attachment contents are not retrieved.
;
; Even though the attachments themselves are not downloaded,
; Chilkat can still provide information such as attachment
; filenames, attachment sizes, and the total message size.
$oBundle = ObjCreate("Chilkat.EmailBundle")
Local $bHeadersOnly = True
$bSuccess = $oImap.FetchMsgSet($bHeadersOnly,$oMessageSet,$oBundle)
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
; Iterate over each email in the bundle and display summary information.
$oEmail = ObjCreate("Chilkat.Email")
Local $sName
Local $sAddr
Local $i = 0
Local $iJ = 0
While $i < $oBundle.MessageCount
$oBundle.EmailAt($i,$oEmail)
; Display the sender and subject line.
ConsoleWrite($oEmail.From & @CRLF)
ConsoleWrite($oEmail.Subject & @CRLF)
; Display all "To" recipients.
$iJ = 0
While $iJ < $oEmail.NumTo
ConsoleWrite("TO: " & $oEmail.GetToName($iJ) & ", " & $oEmail.GetToAddr($iJ) & @CRLF)
$iJ = $iJ + 1
Wend
; Display all "CC" recipients.
$iJ = 0
While $iJ < $oEmail.NumCC
ConsoleWrite("CC: " & $oEmail.GetCcName($iJ) & ", " & $oEmail.GetCcAddr($iJ) & @CRLF)
$iJ = $iJ + 1
Wend
; Display the total size of the email message, including
; the body and all attachments, even though only headers
; were downloaded.
ConsoleWrite($oEmail.Size & @CRLF)
; When full emails are downloaded, attachment information
; is accessed using the email.NumAttachments property and
; the email.GetMailAttach* methods.
;
; However, because this example downloaded headers only,
; attachment metadata must be obtained using the IMAP object.
Local $iNumAttach = $oImap.GetMailNumAttach($oEmail)
$iJ = 0
While $iJ < $iNumAttach
; Display the attachment filename.
ConsoleWrite($oImap.GetMailAttachFilename($oEmail,$iJ) & @CRLF)
; Display the attachment size in bytes.
Local $iAttachSize = $oImap.GetMailAttachSize($oEmail,$iJ)
ConsoleWrite(" size = " & $iAttachSize & " bytes" & @CRLF)
$iJ = $iJ + 1
Wend
ConsoleWrite("--" & @CRLF)
$i = $i + 1
Wend
; Disconnect from the IMAP server.
$bSuccess = $oImap.Disconnect()