Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

List IMAP Mailboxes

List the mailboxes available within an IMAP account.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailboxes.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  This example assumes the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect to an IMAP server.
    ;  Use TLS
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"MY-IMAP-DOMAIN")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Login
    success = CkImap::ckLogin(imap,"MY-IMAP-LOGIN","MY-IMAP-PASSWORD")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    refName.s = ""
    ;  refName is usually set to an empty string.
    ;  A non-empty reference name argument is the name of a mailbox or a level of
    ;  mailbox hierarchy, and indicates the context in which the mailbox
    ;  name is interpreted.

    ;  Select all mailboxes matching this pattern:
    wildcardedMailbox.s = "*"

    subscribed.i = 0

    mboxes.i = CkMailboxes::ckCreate()
    If mboxes.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckMbxList(imap,subscribed,refName,wildcardedMailbox,mboxes)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMailboxes::ckDispose(mboxes)
        ProcedureReturn
    EndIf

    i.i = 0
    While i < CkMailboxes::ckCount(mboxes)
        Debug CkMailboxes::ckGetName(mboxes,i)
        i = i + 1
    Wend

    ;  Sample output looks like this:
    ;  INBOX.vendors.shareit
    ;  INBOX.oldSupport
    ;  INBOX.vendors.paypal
    ;  INBOX.sales
    ;  INBOX.lists
    ;  INBOX.Drafts
    ;  INBOX.vendors.dell
    ;  INBOX.Trash
    ;  INBOX.invoiceRequests
    ;  INBOX.purchases
    ;  INBOX.vendors.inMotion
    ;  INBOX.oldEmail
    ;  INBOX.vendors
    ;  INBOX.lists.python
    ;  INBOX.vendors.myhosting
    ;  INBOX.Templates
    ;  INBOX.friends
    ;  INBOX.bounceSamples
    ;  INBOX.lists.ruby
    ;  INBOX.vendors.peer1
    ;  INBOX.Sent
    ;  INBOX.Junk
    ;  INBOX

    ;  Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMailboxes::ckDispose(mboxes)


    ProcedureReturn
EndProcedure