Sample code for 30+ languages & platforms
PowerBuilder

Set a Single Flag on One IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.SetFlag method, which sets or clears a single flag on one message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is the flag name, and the fourth is the value (1 to set, 0 to clear). This example flags one message as Deleted and then calls Expunge to permanently remove it.

Background: Deleting an IMAP message is a two-step process by design: setting the Deleted flag only marks the message, and Expunge is what actually removes every message so marked. This separation lets a client show "deleted" items and offer an undo before the change becomes permanent. SetFlag handles one flag on one message; use StoreFlags to change several flags at once, or SetFlags to update a whole MessageSet.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_MsgSet
integer li_Uid

li_Success = 0

//  Demonstrates the Imap.SetFlag method, which sets or clears a single flag on one message
//  identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID vs
//  sequence number, the 3rd is the flag name, and the 4th is the value (1 to set, 0 to clear).

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

loo_MsgSet = create oleobject
li_rc = loo_MsgSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("ALL",1,loo_MsgSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    return
end if

if loo_MsgSet.Count > 0 then
    //  Mark the first message for deletion.  The "Deleted" flag is later acted upon by Expunge.
    li_Uid = loo_MsgSet.GetId(0)
    li_Success = loo_Imap.SetFlag(li_Uid,1,"Deleted",1)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_MsgSet
        return
    end if

    //  Permanently remove all messages flagged as Deleted.
    li_Success = loo_Imap.Expunge()
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_MsgSet
        return
    end if

    Write-Debug "Deleted UID " + string(li_Uid)
end if

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



destroy loo_Imap
destroy loo_MsgSet