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

Find an email by subject and fetch it by IMAP UID

See more Email Object Examples

Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.PeekMode = True
$oImap.AutoDownloadAttachments = True
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login("MAIL_USER","MAIL_PASSWORD")
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.SelectMailbox("INBOX")
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$oMatches = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("SUBJECT ""Sample message""",True,$oMatches)
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

If ($oMatches.Count = 0) Then
    ConsoleWrite("No matching message in INBOX." & @CRLF)
    $bSuccess = $oImap.Disconnect()
    If ($bSuccess <> True) Then
        ConsoleWrite($oImap.LastErrorText & @CRLF)
        Exit
    EndIf

    Exit
EndIf

;  The search returned UIDs. Use the same identifier type for the fetch.
Local $iUid = $oMatches.GetId(0)
$oEmail = ObjCreate("Chilkat.Email")
$bSuccess = $oImap.FetchEmail(False,$iUid,True,$oEmail)
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Mailbox INBOX, UIDVALIDITY " & $oImap.UidValidity & ", UID " & $iUid & @CRLF)
$bSuccess = $oImap.Disconnect()
If ($bSuccess <> True) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  The downloaded Email remains usable after disconnecting.
ConsoleWrite("Subject: " & $oEmail.Subject & "  Files: " & $oEmail.NumAttachments & @CRLF)

$bSuccess = $oEmail.SaveEml("imap-received.eml")
If ($bSuccess <> True) Then
    ConsoleWrite($oEmail.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Saved imap-received.eml." & @CRLF)