Sample code for 30+ languages & platforms
Xojo Plugin

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

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

Dim imap As New Chilkat.Imap

imap.Ssl = True
imap.Port = 993

success = imap.Connect("imap.example.com")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.Login("user@example.com","myPassword")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.SelectMailbox("Inbox")
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

Dim msgSet As New Chilkat.MessageSet
success = imap.QueryMbx("UNSEEN",True,msgSet)
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

Dim bundle As New Chilkat.EmailBundle
success = imap.FetchMsgSet(True,msgSet,bundle)
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

Dim email As New Chilkat.Email
Dim i As Int32
For i = 0 To bundle.MessageCount - 1
    success = bundle.EmailAt(i,email)
    //  Mark this message as Flagged on the server.  A value of 1 sets the flag; 0 would clear it.
    success = imap.SetMailFlag(email,"Flagged",1)
    If (success = False) Then
        System.DebugLog(imap.LastErrorText)
        Return
    End If

Next

System.DebugLog("Flagged " + Str(bundle.MessageCount) + " messages.")

success = imap.Disconnect()
If (success = False) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If