Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

//  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).

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

loMsgSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",.T.,loMsgSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    return
endif

if (loMsgSet.Count > 0) then
    //  Mark the first message for deletion.  The "Deleted" flag is later acted upon by Expunge.
    lnUid = loMsgSet.GetId(0)
    llSuccess = loImap.SetFlag(lnUid,.T.,"Deleted",1)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMsgSet
        return
    endif

    //  Permanently remove all messages flagged as Deleted.
    llSuccess = loImap.Expunge()
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMsgSet
        return
    endif

    ? "Deleted UID " + str(lnUid)
endif

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    return
endif



release loImap
release loMsgSet