Sample code for 30+ languages & platforms
AutoIt

Store One or More Flags on a Single Message

See more IMAP Examples

Demonstrates the Chilkat Imap.StoreFlags method, which sets or clears one or more flags on a single message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is a space-separated list of flag names, and the fourth is the value (1 to set, 0 to clear). This example applies the Seen and Flagged flags to one message by UID.

Background: StoreFlags is the single-message counterpart to SetFlags and lets you change several flags at once by listing them space-separated (without leading backslashes). Because the ID can be either a UID or a sequence number, the bUid argument must match how you obtained it — a UID from a UID search, or a sequence number from a sequence-based operation. UIDs are the safer choice since they remain valid as the mailbox changes.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the Imap.StoreFlags method, which sets or clears one or more flags on a single
;  message identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID
;  vs sequence number, the 3rd is a space-separated list of flag names, and the 4th is the
;  value (1 to set, 0 to clear).

$oImap = ObjCreate("Chilkat.Imap")

$oImap.Ssl = True
$oImap.Port = 993

$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.Login("user@example.com","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oImap.SelectMailbox("Inbox")
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

;  Get the UIDs of all messages.
$oMsgSet = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("ALL",True,$oMsgSet)
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf

If ($oMsgSet.Count > 0) Then
    ;  Apply the Seen and Flagged flags to the first message.  bUid is True because the ID
    ;  came from a UID search.
Local $iUid = $oMsgSet.GetId(0)
    $bSuccess = $oImap.StoreFlags($iUid,True,"Seen Flagged",1)
    If ($bSuccess = False) Then
        ConsoleWrite($oImap.LastErrorText & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Updated flags on UID " & $iUid & @CRLF)
EndIf

$bSuccess = $oImap.Disconnect()
If ($bSuccess = False) Then
    ConsoleWrite($oImap.LastErrorText & @CRLF)
    Exit
EndIf