Sample code for 30+ languages & platforms
PureBasic

Expunge Deleted Messages from a Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.Expunge method, which permanently removes all messages marked with the \Deleted flag from the currently selected mailbox. The mailbox remains selected. This example marks a message deleted with SetFlag and then expunges.

Background: IMAP deletion is two-phase, much like POP3. First a message is marked with the \Deleted flag (it still exists, just tagged); then EXPUNGE permanently removes every flagged message and renumbers the rest. Splitting the two steps lets a client mark several messages and remove them together, or unmark (undelete) before committing. Expunge keeps the mailbox selected; ExpungeAndClose does the same and then closes it.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Imap.Expunge method, which permanently removes all messages marked with
    ;  the \Deleted flag from the currently selected mailbox.  The mailbox remains selected.

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

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Select the mailbox to operate on.
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Mark message (sequence number 1) with the \Deleted flag.  The 4th argument (1) sets the
    ;  flag; the 2nd argument (0) means the id is a sequence number, not a UID.
    success = CkImap::ckSetFlag(imap,1,0,"\Deleted",1)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Permanently remove all \Deleted messages from the selected mailbox.
    success = CkImap::ckExpunge(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    Debug "Expunged the deleted messages."

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)


    ProcedureReturn
EndProcedure