Xbase++ Requires Chilkat v11.0.0+
Xbase++
Delete Email Individually (One at a time) from an IMAP Mailbox
Downloads email from an IMAP mailbox and deletes emails individually (one by one).Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oBundle
LOCAL nHeadersOnly
LOCAL oEmail
LOCAL i
LOCAL nNumEmails
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oImap := CreateObject("Chilkat.Imap")
// 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")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// We can choose to fetch UIDs or sequence numbers.
nFetchUids := 1
// Get the message IDs of all the emails in the mailbox
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
RETURN
ENDIF
// Fetch the emails into a bundle object:
oBundle := CreateObject("Chilkat.EmailBundle")
nHeadersOnly := 0
nSuccess := oImap:FetchMsgSet(nHeadersOnly, oMessageSet, oBundle)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
RETURN
ENDIF
// To mark a complete set of emails for deletion, call SetFlags:
nSuccess := oImap:SetFlags(oMessageSet, "Deleted", 1)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
RETURN
ENDIF
// Messages can also be marked for deletion individually:
// Loop over the bundle and mark each message for deletion.
oEmail := CreateObject("Chilkat.Email")
i := 0
nNumEmails := oBundle:MessageCount
DO WHILE i < nNumEmails
oBundle:EmailAt(i, oEmail)
? oEmail:From
? oEmail:Subject
// To delete this email, set the "Deleted" flag to 1.
// The email is not actually deleted until Expunge or
// ExpungeAndClose is called.
nSuccess := oImap:SetMailFlag(oEmail, "Deleted", 1)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()
RETURN
ENDIF
? "--"
i := i + 1
ENDDO
nSuccess := oImap:ExpungeAndClose()
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()
RETURN
ENDIF
// Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()
oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()