Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL cRefName
LOCAL cWildcardedMailbox
LOCAL nSubscribed
LOCAL oMboxes
LOCAL i

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  First examine the already subscribed mailboxes.
cRefName := ""
cWildcardedMailbox := "*"
nSubscribed := 1

oMboxes := CreateObject("Chilkat.Mailboxes")
nSuccess := oImap:MbxList(nSubscribed, cRefName, cWildcardedMailbox, oMboxes)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMboxes:destroy()
    RETURN
ENDIF

i := 0
DO WHILE i < oMboxes:Count
    ? oMboxes:GetName(i)
    i := i + 1
ENDDO
? "----"

//  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".
nSuccess := oImap:Unsubscribe("INBOX/misc/solutions")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMboxes:destroy()
    RETURN
ENDIF

//  Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is missing.
nSuccess := oImap:MbxList(nSubscribed, "", "INBOX/misc*", oMboxes)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMboxes:destroy()
    RETURN
ENDIF

i := 0
DO WHILE i < oMboxes:Count
    ? oMboxes:GetName(i)
    i := i + 1
ENDDO
? "----"

//  Re-subscribe to "INBOX/misc/solutions".
nSuccess := oImap:Subscribe("INBOX/misc/solutions")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMboxes:destroy()
    RETURN
ENDIF

//  Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is back in the list.
nSuccess := oImap:MbxList(nSubscribed, "", "INBOX/misc*", oMboxes)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMboxes:destroy()
    RETURN
ENDIF

i := 0
DO WHILE i < oMboxes:Count
    ? oMboxes:GetName(i)
    i := i + 1
ENDDO
? "----"

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oMboxes:destroy()