Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkImapW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkImapW imap;

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect(L"imap.example.com");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    success = imap.Login(L"user@example.com",L"myPassword");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    //  Select the mailbox to operate on.
    success = imap.SelectMailbox(L"Inbox");
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    //  Mark message (sequence number 1) with the \Deleted flag.  The 4th argument (1) sets the
    //  flag; the 2nd argument (false) means the id is a sequence number, not a UID.
    success = imap.SetFlag(1,false,L"\Deleted",1);
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    //  Permanently remove all \Deleted messages from the selected mailbox.
    success = imap.Expunge();
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }

    wprintf(L"Expunged the deleted messages.\n");

    success = imap.Disconnect();
    if (success == false) {
        wprintf(L"%s\n",imap.lastErrorText());
        return;
    }
    }