Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet
LOCAL n
LOCAL i
nSuccess := 0
// 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
// 1, the identifiers are UIDs; otherwise they are sequence numbers.
oImap := CreateObject("Chilkat.Imap")
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// Search for unseen (unread) messages, returning their UIDs.
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("UNSEEN", 1, oMsgSet)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
RETURN
ENDIF
// Iterate the matching message identifiers.
n := oMsgSet:Count
? "Matching messages: " + Str(n)
FOR i := 0 TO n - 1
? "UID: " + Str(oMsgSet:GetId(i))
NEXT
nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
RETURN
ENDIF
oImap:destroy()
oMsgSet:destroy()