Sample code for 30+ languages & platforms
Lianja

Delete All IMAP Email

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

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

// Turn on session logging:
loImap.KeepSessionLog = .T.

// Connect to an IMAP server.
// Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Login
llSuccess = loImap.Login("myLogin","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox.RubyMail")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Get the complete set of Uids for email in the selected mailbox.
loMessageSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",.T.,loMessageSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

// Set the Deleted flag for each message.
// (ExpungeAndClose must be called to finalize the delete.)
llSuccess = loImap.SetFlags(loMessageSet,"Deleted",1)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

// Alternatively, the Deleted flag may be set for each UID
// individiually, but this is less efficient:
i = 0
n = loMessageSet.Count
do while i < n
    llSuccess = loImap.SetFlag(loMessageSet.GetId(i),loMessageSet.HasUids,"Deleted",1)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMessageSet
        return
    endif

    i = i + 1
enddo

// Expunge and close the mailbox.
llSuccess = loImap.ExpungeAndClose()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

// Display the session log.
? loImap.SessionLog

// Disconnect from the IMAP server.
llSuccess = loImap.Disconnect()


release loImap
release loMessageSet