Sample code for 30+ languages & platforms
PowerBuilder

Expunge Deleted Messages and Close the Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.ExpungeAndClose method, which permanently removes all messages marked with \Deleted from the selected mailbox and then closes the mailbox. This example marks a message deleted and then expunges-and-closes.

Background: This combines two actions into one call: the permanent removal of \Deleted messages (see Expunge) followed by closing the mailbox (see CloseMailbox), returning the session to the authenticated-but-no-mailbox-selected state while staying connected. It's the natural way to finish working with a mailbox after cleaning it up, before selecting another or logging out.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap

li_Success = 0

//  Demonstrates the Imap.ExpungeAndClose method, which permanently removes all messages
//  marked with \Deleted from the selected mailbox and then closes the mailbox.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Mark message (sequence number 1) with the \Deleted flag.
li_Success = loo_Imap.SetFlag(1,0,"\Deleted",1)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Remove the \Deleted messages and close the mailbox in one step.
li_Success = loo_Imap.ExpungeAndClose()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

Write-Debug "Expunged and closed the mailbox."

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if



destroy loo_Imap