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

Delete Individual Emails from POP3 Mailbox

Demonstrates how to delete individual emails from a POP3 mailbox.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL oBundle
LOCAL nKeepOnServer
LOCAL nHeadersOnly
LOCAL nNumBodyLines
LOCAL oEmail
LOCAL i

nSuccess := 0

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

//  The mailman object is used for receiving (POP3) 
//  and sending (SMTP) email.
oMailman := CreateObject("Chilkat.MailMan")

//  Set the POP3 server's hostname
oMailman:MailHost := "pop.example.com"

//  Set the POP3 login/password.
oMailman:PopUsername := "myLogin"
oMailman:PopPassword := "myPassword"

//  Set the ImmediateDelete property = 0
//  When the DeleteEmail method is called, the POP3 session
//  will remain open and the emails will be deleted all at once
//  when the session closes.
oMailman:ImmediateDelete := 0

//  Download email headers.  
oBundle := CreateObject("Chilkat.EmailBundle")
nKeepOnServer := 1
nHeadersOnly := 1
nNumBodyLines := 1
nSuccess := oMailman:FetchAll(nKeepOnServer, nHeadersOnly, nNumBodyLines, oBundle)
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oBundle:destroy()
    RETURN
ENDIF

oEmail := CreateObject("Chilkat.Email")
i := 0
DO WHILE i < oBundle:MessageCount
    oBundle:EmailAt(i, oEmail)

    ? "From: " + oEmail:From
    ? "Subject: " + oEmail:Subject

    //  If you decide the email is to be deleted, call DeleteEmail.
    //  This marks the email for deletion on the POP3 server.
    nSuccess := oMailman:DeleteEmail(oEmail)
    IF (nSuccess == 0)
        ? oMailman:LastErrorText
        oMailman:destroy()
        oBundle:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    i := i + 1
ENDDO

//  Make sure the POP3 session is ended to finalize the deletes.
nSuccess := oMailman:Pop3EndSession()
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    oBundle:destroy()
    oEmail:destroy()
    RETURN
ENDIF

? "Success!"

oMailman:destroy()
oBundle:destroy()
oEmail:destroy()