Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL nSubscribedOnly
LOCAL oMailboxes
LOCAL n
LOCAL i
LOCAL cName
LOCAL cFlags
LOCAL nNumFlags
LOCAL j
LOCAL cFlag
LOCAL nSentIndex
nSuccess := 0
// 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.
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
// Populate a Mailboxes object with the full list of mailboxes. The first argument requests
// all mailboxes (not subscribed-only) and "*" matches any name.
nSubscribedOnly := 0
oMailboxes := CreateObject("Chilkat.Mailboxes")
nSuccess := oImap:MbxList(nSubscribedOnly, "", "*", oMailboxes)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMailboxes:destroy()
RETURN
ENDIF
// Loop over each mailbox in the returned collection.
n := oMailboxes:Count
? "Number of mailboxes: " + Str(n)
FOR i := 0 TO n - 1
cName := oMailboxes:GetName(i)
? "Mailbox: " + cName
// Whether the mailbox can be selected or examined for message access.
IF (oMailboxes:IsSelectable(i))
? " Selectable: yes"
ELSE
? " Selectable: no"
ENDIF
// Whether the server marked the mailbox as possibly having new messages.
IF (oMailboxes:IsMarked(i))
? " Marked: yes"
ENDIF
// Whether the mailbox has, or may contain, child mailboxes.
IF (oMailboxes:HasInferiors(i))
? " May have child mailboxes"
ENDIF
// 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 (oMailboxes:HasFlag(i, "\HasChildren"))
? " Has child mailboxes"
ENDIF
// Get all of the mailbox's flags as a comma-separated string.
cFlags := oMailboxes:GetFlags(i)
? " Flags: " + cFlags
// Iterate the flags individually.
nNumFlags := oMailboxes:GetNumFlags(i)
FOR j := 0 TO nNumFlags - 1
cFlag := oMailboxes:GetNthFlag(i, j)
? " Flag " + Str(j) + ": " + cFlag
NEXT
NEXT
// Look up a mailbox by name. Returns -1 if no mailbox with that name is present.
nSentIndex := oMailboxes:GetMailboxIndex("Sent")
IF (nSentIndex >= 0)
? "The Sent mailbox is at index " + Str(nSentIndex)
ENDIF
nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMailboxes:destroy()
RETURN
ENDIF
oImap:destroy()
oMailboxes:destroy()