Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_SubscribedOnly
oleobject loo_Mailboxes
integer n
integer i
string ls_Name
string ls_Flags
integer li_NumFlags
integer j
string ls_Flag
integer li_SentIndex

li_Success = 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.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    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.
li_SubscribedOnly = 0
loo_Mailboxes = create oleobject
li_rc = loo_Mailboxes.ConnectToNewObject("Chilkat.Mailboxes")

li_Success = loo_Imap.MbxList(li_SubscribedOnly,"","*",loo_Mailboxes)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Mailboxes
    return
end if

//  Loop over each mailbox in the returned collection.
n = loo_Mailboxes.Count
Write-Debug "Number of mailboxes: " + string(n)

for i = 0 to n - 1
    ls_Name = loo_Mailboxes.GetName(i)
    Write-Debug "Mailbox: " + ls_Name

    //  Whether the mailbox can be selected or examined for message access.
    if loo_Mailboxes.IsSelectable(i) then
        Write-Debug "  Selectable: yes"
    else
        Write-Debug "  Selectable: no"
    end if

    //  Whether the server marked the mailbox as possibly having new messages.
    if loo_Mailboxes.IsMarked(i) then
        Write-Debug "  Marked: yes"
    end if

    //  Whether the mailbox has, or may contain, child mailboxes.
    if loo_Mailboxes.HasInferiors(i) then
        Write-Debug "  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 loo_Mailboxes.HasFlag(i,"\HasChildren") then
        Write-Debug "  Has child mailboxes"
    end if

    //  Get all of the mailbox's flags as a comma-separated string.
    ls_Flags = loo_Mailboxes.GetFlags(i)
    Write-Debug "  Flags: " + ls_Flags

    //  Iterate the flags individually.
    li_NumFlags = loo_Mailboxes.GetNumFlags(i)

    for j = 0 to li_NumFlags - 1
        ls_Flag = loo_Mailboxes.GetNthFlag(i,j)
        Write-Debug "    Flag " + string(j) + ": " + ls_Flag
    next
next

//  Look up a mailbox by name.  Returns -1 if no mailbox with that name is present.
li_SentIndex = loo_Mailboxes.GetMailboxIndex("Sent")
if li_SentIndex >= 0 then
    Write-Debug "The Sent mailbox is at index " + string(li_SentIndex)
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Mailboxes
    return
end if



destroy loo_Imap
destroy loo_Mailboxes