Sample code for 30+ languages & platforms
AutoIt

Get the Number of IMAP Attachments

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailNumAttach method, which returns the number of ordinary attachments on a message. The only argument is the Email. This example fetches only the headers and reports the attachment count.

Background: A header-only fetch downloads no attachment bodies, so the Email object's own downloaded-attachment count can be 0. GetMailNumAttach instead reads the ckx-imap-numAttach metadata, giving the true count — which is what you want when building a message list that shows whether each message has attachments.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the Imap.GetMailNumAttach method, which returns the number of ordinary
;  attachments on a message.  The only argument is the Email.

$oImap = ObjCreate("Chilkat.Imap")

$oImap.Ssl = True
$oImap.Port = 993

$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

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

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

;  Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
;  metadata describing the attachments is present, so the attachment info methods still work.
Local $bHeadersOnly = True
Local $bUseUid = False
Local $iSeqNum = 1
$oEmail = ObjCreate("Chilkat.Email")
$bSuccess = $oImap.FetchEmail($bHeadersOnly,$iSeqNum,$bUseUid,$oEmail)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

Local $iNumAttach = $oImap.GetMailNumAttach($oEmail)
ConsoleWrite("This message has " & $iNumAttach & " attachment(s)." & @CRLF)

$bSuccess = $oImap.Disconnect()
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf