Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let imap = CkoImap()!

    imap.ssl = true
    imap.port = 993

    success = imap.connect(hostname: "imap.example.com")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    success = imap.login(login: "user@example.com", password: "myPassword")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    //  List all mailboxes.  The first argument false requests all (not subscribed-only); "*" matches any name.
    let mailboxes = CkoMailboxes()!
    success = imap.mbxList(subscribed: false, reference: "", mbxPattern: "*", mboxes: mailboxes)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    var n: Int = mailboxes.count.intValue
    print("Mailboxes: \(n)")
    var i: Int
    for i = 0; i <= n - 1; i++ {
        print("\(mailboxes.getName(index: i)!)")
    }

    success = imap.ERROR: Swift property not found: Imap.Disconnect
ERROR: ";" expected


}