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

Subscribe to Mailboxes and List Subscribed Mailboxes

See more IMAP Examples

Demonstrates how to list subscribed mailboxes, and unsubscribe/subscribe to a mailbox.

Chilkat Lua Downloads

Lua

    -- In the following call to loadlib, change the path (./chilkat.dll) to the relative or absolute directory where the chilkat.dll, chilkat.so, or chilkat.dylib is located.
    chilkat = assert(package.loadlib("./chilkat.dll", "luaopen_chilkat"))()
    print(chilkat._VERSION)

    local success = false

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

    local imap = chilkat.newImap{}

    --  Connect to an IMAP server.
    --  Use TLS
    imap:setSsl(true)
    imap:setPort(993)
    success = imap:Connect("imap.example.com")
    if success == false then
        print(imap:LastErrorText())

    end

    --  Login
    success = imap:Login("myLogin","myPassword")
    if success == false then
        print(imap:LastErrorText())

    end

    --  First examine the already subscribed mailboxes.
    local refName = ""
    local wildcardedMailbox = "*"
    local subscribed = true

    local mboxes = chilkat.newMailboxes{}
    success = imap:MbxList(subscribed,refName,wildcardedMailbox,mboxes)
    if success == false then
        print(imap:LastErrorText())

    end

    local i = 0
    while i < mboxes:Count() do
        print(mboxes:GetName(i))
        i = i + 1
    end

    print("----")

    --  Sample output.

    --  INBOX
    --  INBOX/recent
    --  INBOX/misc
    --  INBOX/misc/solutions
    --  ...
    --  Outbox
    --  Deleted Items
    --  Sent Items
    --  Drafts
    --  Junk E-mail
    --  Trash
    --  Sent
    --  Templates

    --  Let's unsubscribe to "INBOX/misc/solutions".
    success = imap:Unsubscribe("INBOX/misc/solutions")
    if success == false then
        print(imap:LastErrorText())

    end

    --  Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is missing.
    success = imap:MbxList(subscribed,"","INBOX/misc*",mboxes)
    if success == false then
        print(imap:LastErrorText())

    end

    i = 0
    while i < mboxes:Count() do
        print(mboxes:GetName(i))
        i = i + 1
    end

    print("----")

    --  Re-subscribe to "INBOX/misc/solutions".
    success = imap:Subscribe("INBOX/misc/solutions")
    if success == false then
        print(imap:LastErrorText())

    end

    --  Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is back in the list.
    success = imap:MbxList(subscribed,"","INBOX/misc*",mboxes)
    if success == false then
        print(imap:LastErrorText())

    end

    i = 0
    while i < mboxes:Count() do
        print(mboxes:GetName(i))
        i = i + 1
    end

    print("----")

    --  Disconnect from the IMAP server.
    success = imap:Disconnect()