B4X
B4X
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 B4X Downloads
Dim success As Boolean = 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.
Dim imap As ChilkatImap
imap.Initialize("imap")
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.example.com")
If success = False Then
Log(imap.LastErrorText)
Return
End If
success = imap.Login("user@example.com", "myPassword")
If success = False Then
Log(imap.LastErrorText)
Return
End If
success = imap.SelectMailbox("Inbox")
If success = False Then
Log(imap.LastErrorText)
Return
End If
' Search for unseen (unread) messages, returning their UIDs.
Dim msgSet As ChilkatMessageSet
msgSet.Initialize
success = imap.QueryMbx("UNSEEN", True, msgSet)
If success = False Then
Log(imap.LastErrorText)
Return
End If
' Iterate the matching message identifiers.
Dim n As Int = msgSet.Count
Log("Matching messages: " & n)
Dim i As Int
For i = 0 To n - 1
Log("UID: " & msgSet.GetId(i))
Next
success = imap.Disconnect
If success = False Then
Log(imap.LastErrorText)
Return
End If