AutoIt
AutoIt
Search an IMAP Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.QueryMbx method, which searches the selected mailbox using IMAP search criteria and stores the matching message identifiers in a MessageSet. If bUid is true, the identifiers are UIDs; otherwise they are sequence numbers. This example finds unseen messages and lists their UIDs.
Background: IMAP search runs on the server, so you can find matching messages without downloading them. The criteria string is standard IMAP search syntax — for example
UNSEEN, FROM "alice@example.com", SUBJECT "invoice", or SINCE 1-Jan-2026 — and terms can be combined. The result is a MessageSet of identifiers you then fetch, flag, copy, or move. Requesting UIDs (bUid = true) is preferred because UIDs remain valid across sessions.Chilkat AutoIt Downloads
Local $bSuccess = False
; Demonstrates the Imap.QueryMbx method, which searches the selected mailbox using IMAP
; search criteria and stores the matching message identifiers in a MessageSet. If bUid is
; True, the identifiers are UIDs; otherwise they are sequence numbers.
$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
; Search for unseen (unread) messages, returning their UIDs.
$oMsgSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("UNSEEN",True,$oMsgSet)
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
; Iterate the matching message identifiers.
Local $iN = $oMsgSet.Count
ConsoleWrite("Matching messages: " & $iN & @CRLF)
Local $i
For $i = 0 To $iN - 1
ConsoleWrite("UID: " & $oMsgSet.GetId($i) & @CRLF)
Next
$bSuccess = $oImap.Disconnect()
If ($bSuccess = False) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf