Sample code for 30+ languages & platforms
DataFlex

List and Inspect IMAP Mailboxes

See more IMAP Examples

Demonstrates the Chilkat Mailboxes class, a read-only result collection populated by Imap.MbxList. This example connects to an IMAP server, lists all mailboxes into a Mailboxes object, then loops from 0 to Count - 1 and inspects each mailbox: its name (GetName), whether it is selectable (IsSelectable) or marked (IsMarked), whether it may have child mailboxes (HasInferiors), a specific flag (HasFlag), and all of its flags both as a string (GetFlags) and individually (GetNumFlags with GetNthFlag). Finally it looks up a mailbox by name with GetMailboxIndex.

Background: Mailboxes is how an application renders an IMAP folder tree. Because it is read-only, you never construct it directly — you receive it from MbxList and read its entries by index. Mailbox names come back as normal Unicode text (Chilkat handles the modified UTF-7 / IMAP UTF-8 encoding at the protocol level), so a name from GetName can be passed straight to methods like SelectMailbox. Note that HasInferiors is often true: it means children exist or may be created, which is not the same as \HasChildren.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Boolean iSubscribedOnly
    Variant vMailboxes
    Handle hoMailboxes
    Integer n
    Integer i
    String sName
    String sFlags
    Integer iNumFlags
    Integer j
    String sFlag
    Integer iSentIndex
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates using Chilkat.Imap to populate a Mailboxes object (via MbxList), then looping
    //  over the collection to inspect each mailbox's name and attributes.  Mailboxes is a read-only
    //  result collection -- applications inspect the entries but do not add, remove, or modify them.

    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

    //  Populate a Mailboxes object with the full list of mailboxes.  The first argument requests
    //  all mailboxes (not subscribed-only) and "*" matches any name.
    Move False To iSubscribedOnly
    Get Create (RefClass(cComChilkatMailboxes)) To hoMailboxes
    If (Not(IsComObjectCreated(hoMailboxes))) Begin
        Send CreateComObject of hoMailboxes
    End
    Get pvComObject of hoMailboxes to vMailboxes
    Get ComMbxList Of hoImap iSubscribedOnly "" "*" vMailboxes To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Loop over each mailbox in the returned collection.
    Get ComCount Of hoMailboxes To n
    Showln "Number of mailboxes: " n

    For i From 0 To (n - 1)
        Get ComGetName Of hoMailboxes i To sName
        Showln "Mailbox: " sName

        //  Whether the mailbox can be selected or examined for message access.
        Get ComIsSelectable Of hoMailboxes i To bTemp1
        If (bTemp1) Begin
            Showln "  Selectable: yes"
        End
        Else Begin
            Showln "  Selectable: no"
        End

        //  Whether the server marked the mailbox as possibly having new messages.
        Get ComIsMarked Of hoMailboxes i To bTemp1
        If (bTemp1) Begin
            Showln "  Marked: yes"
        End

        //  Whether the mailbox has, or may contain, child mailboxes.
        Get ComHasInferiors Of hoMailboxes i To bTemp1
        If (bTemp1) Begin
            Showln "  May have child mailboxes"
        End

        //  Check for a specific flag by name.  Include the leading backslash for a system flag;
        //  in CkScript a literal backslash in a string is written as "\\".
        Get ComHasFlag Of hoMailboxes i "\HasChildren" To bTemp1
        If (bTemp1) Begin
            Showln "  Has child mailboxes"
        End

        //  Get all of the mailbox's flags as a comma-separated string.
        Get ComGetFlags Of hoMailboxes i To sFlags
        Showln "  Flags: " sFlags

        //  Iterate the flags individually.
        Get ComGetNumFlags Of hoMailboxes i To iNumFlags

        For j From 0 To (iNumFlags - 1)
            Get ComGetNthFlag Of hoMailboxes i j To sFlag
            Showln "    Flag " j ": " sFlag
        Loop

    Loop

    //  Look up a mailbox by name.  Returns -1 if no mailbox with that name is present.
    Get ComGetMailboxIndex Of hoMailboxes "Sent" To iSentIndex
    If (iSentIndex >= 0) Begin
        Showln "The Sent mailbox is at index " iSentIndex
    End

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



End_Procedure