Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vMsgSet
    Handle hoMsgSet
    Integer n
    Integer i
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Search for unseen (unread) messages, returning their UIDs.
    Get Create (RefClass(cComChilkatMessageSet)) To hoMsgSet
    If (Not(IsComObjectCreated(hoMsgSet))) Begin
        Send CreateComObject of hoMsgSet
    End
    Get pvComObject of hoMsgSet to vMsgSet
    Get ComQueryMbx Of hoImap "UNSEEN" True vMsgSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Iterate the matching message identifiers.
    Get ComCount Of hoMsgSet To n
    Showln "Matching messages: " n

    For i From 0 To (n - 1)
        Get ComGetId Of hoMsgSet i To iTemp1
        Showln "UID: " iTemp1
    Loop

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure