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

Delete All IMAP Email

Demonstrates two ways to delete all email in a mailbox on an IMAP server.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMessageSet
LOCAL i
LOCAL n

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Turn on session logging:
oImap:KeepSessionLog := 1

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

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox.RubyMail")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Get the complete set of Uids for email in the selected mailbox.
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", 1, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Set the Deleted flag for each message.
//  (ExpungeAndClose must be called to finalize the delete.)
nSuccess := oImap:SetFlags(oMessageSet, "Deleted", 1)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Alternatively, the Deleted flag may be set for each UID
//  individiually, but this is less efficient:
i := 0
n := oMessageSet:Count
DO WHILE i < n
    nSuccess := oImap:SetFlag(oMessageSet:GetId(i), oMessageSet:HasUids, "Deleted", 1)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMessageSet:destroy()
        RETURN
    ENDIF

    i := i + 1
ENDDO

//  Expunge and close the mailbox.
nSuccess := oImap:ExpungeAndClose()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Display the session log.
? oImap:SessionLog

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

oImap:destroy()
oMessageSet:destroy()