Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
imap: HCkImap;
msgSet: HCkMessageSet;
n: Integer;
i: Integer;
begin
success := 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
// 1, the identifiers are UIDs; otherwise they are sequence numbers.
imap := CkImap_Create();
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
success := CkImap_Login(imap,'user@example.com','myPassword');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
success := CkImap_SelectMailbox(imap,'Inbox');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Search for unseen (unread) messages, returning their UIDs.
msgSet := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'UNSEEN',True,msgSet);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Iterate the matching message identifiers.
n := CkMessageSet_getCount(msgSet);
Memo1.Lines.Add('Matching messages: ' + IntToStr(n));
for i := 0 to n - 1 do
begin
Memo1.Lines.Add('UID: ' + IntToStr(CkMessageSet_GetId(msgSet,i)));
end;
success := CkImap_Disconnect(imap);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
CkImap_Dispose(imap);
CkMessageSet_Dispose(msgSet);