Sample code for 30+ languages & platforms
Tcl

List IMAP Mailboxes

See more IMAP Examples

Demonstrates the Chilkat Imap.MbxList method, which lists matching mailboxes and appends them to a Mailboxes object. The first argument selects subscribed-only, the second is a reference name, the third is the mailbox name pattern, and the fourth is the Mailboxes object. This example lists all mailboxes and prints each name. (The object is not cleared on success, so avoid reusing one across calls to prevent duplicates.)

Background: This corresponds to the IMAP LIST command (or LSUB when subscribed-only is requested). The pattern uses IMAP wildcards: * matches across hierarchy levels while % matches within a single level, so Archive/% lists just the direct children of Archive. Each returned mailbox also carries flags you can inspect — for example HasInferiors (has sub-folders) and IsSelectable — which is how a client renders a folder tree.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.MbxList method, which lists matching mailboxes and appends them to a
#  Mailboxes object.  The 1st argument selects subscribed-only, the 2nd is a reference name,
#  the 3rd is the mailbox pattern, and the 4th is the Mailboxes object.

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  List all mailboxes.  The first argument 0 requests all (not subscribed-only); "*" matches any name.
set mailboxes [new_CkMailboxes]

set success [CkImap_MbxList $imap 0 "" "*" $mailboxes]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMailboxes $mailboxes
    exit
}

set n [CkMailboxes_get_Count $mailboxes]
puts "Mailboxes: $n"

for {set i 0} {$i <= [expr $n - 1]} {incr i} {
    puts [CkMailboxes_getName $mailboxes $i]
}

set success [CkImap_get_Disconnect $imap]ERROR: ";" expected


delete_CkImap $imap
delete_CkMailboxes $mailboxes