Sample code for 30+ languages & platforms
Xojo Plugin

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

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

Dim imap As New Chilkat.Imap

imap.Ssl = True
imap.Port = 993

success = imap.Connect("imap.example.com")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.Login("user@example.com","myPassword")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

//  Populate a Mailboxes object with the full list of mailboxes.  The first argument requests
//  all mailboxes (not subscribed-only) and "*" matches any name.
Dim subscribedOnly As Boolean
subscribedOnly = False
Dim mailboxes As New Chilkat.Mailboxes
success = imap.MbxList(subscribedOnly,"","*",mailboxes)
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

//  Loop over each mailbox in the returned collection.
Dim n As Int32
n = mailboxes.Count
System.DebugLog("Number of mailboxes: " + Str(n))

Dim i As Int32
For i = 0 To n - 1
    Dim name As String
    name = mailboxes.GetName(i)
    System.DebugLog("Mailbox: " + name)

    //  Whether the mailbox can be selected or examined for message access.
    If (mailboxes.IsSelectable(i)) Then
        System.DebugLog("  Selectable: yes")
    Else
        System.DebugLog("  Selectable: no")
    End If

    //  Whether the server marked the mailbox as possibly having new messages.
    If (mailboxes.IsMarked(i)) Then
        System.DebugLog("  Marked: yes")
    End If

    //  Whether the mailbox has, or may contain, child mailboxes.
    If (mailboxes.HasInferiors(i)) Then
        System.DebugLog("  May have child mailboxes")
    End If

    //  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 "\\".
    If (mailboxes.HasFlag(i,"\HasChildren")) Then
        System.DebugLog("  Has child mailboxes")
    End If

    //  Get all of the mailbox's flags as a comma-separated string.
    Dim flags As String
    flags = mailboxes.GetFlags(i)
    System.DebugLog("  Flags: " + flags)

    //  Iterate the flags individually.
    Dim numFlags As Int32
    numFlags = mailboxes.GetNumFlags(i)
    Dim j As Int32
    For j = 0 To numFlags - 1
        Dim flag As String
        flag = mailboxes.GetNthFlag(i,j)
        System.DebugLog("    Flag " + Str(j) + ": " + flag)
    Next
Next

//  Look up a mailbox by name.  Returns -1 if no mailbox with that name is present.
Dim sentIndex As Int32
sentIndex = mailboxes.GetMailboxIndex("Sent")
If (sentIndex >= 0) Then
    System.DebugLog("The Sent mailbox is at index " + Str(sentIndex))
End If

success = imap.Disconnect()
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If