Sample code for 30+ languages & platforms
PowerBuilder

Set an IMAP Flag Using an Email Object

See more IMAP Examples

Demonstrates the Chilkat Imap.SetMailFlag method, which sets or clears a flag on the server for the message represented by an Email object. The first argument is the Email, the second is the flag name, and the third is the value (1 to set, 0 to clear). This example flags each fetched message as Flagged on the server.

Background: SetMailFlag is convenient when you already hold an Email from a fetch: Chilkat uses the UID it recorded on that object to target the right message on the server, so you do not have to track IDs yourself. It updates both the server and the local Email object, keeping the two in sync. Under the hood this is the same STORE operation as SetFlag — just addressed by object rather than by raw ID.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_MsgSet
oleobject loo_Bundle
oleobject loo_Email
integer i

li_Success = 0

//  Demonstrates the Imap.SetMailFlag method, which sets or clears a flag on the server for the
//  message represented by an Email object.  The 1st argument is the Email, the 2nd is the flag
//  name, and the 3rd 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("UNSEEN",1,loo_MsgSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    return
end if

loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")

li_Success = loo_Imap.FetchMsgSet(1,loo_MsgSet,loo_Bundle)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    destroy loo_Bundle
    return
end if

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

for i = 0 to loo_Bundle.MessageCount - 1
    li_Success = loo_Bundle.EmailAt(i,loo_Email)
    //  Mark this message as Flagged on the server.  A value of 1 sets the flag; 0 would clear it.
    li_Success = loo_Imap.SetMailFlag(loo_Email,"Flagged",1)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_MsgSet
        destroy loo_Bundle
        destroy loo_Email
        return
    end if

next

Write-Debug "Flagged " + string(loo_Bundle.MessageCount) + " messages."

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



destroy loo_Imap
destroy loo_MsgSet
destroy loo_Bundle
destroy loo_Email