ImapSetMessageFlags Method (String, Boolean, String, MessageFlagAction, Boolean)
Sets or resets the specified flags for the specified messages in the currently selected folder.

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public bool SetMessageFlags(
	string messageIndexSet,
	bool indexIsUid,
	string flags,
	MessageFlagAction action,
	bool silentMode
)

Parameters

messageIndexSet
Type: SystemString
A message sequence string containing ordinal message numbers or UIDs. Can be composed manually or using ToString.
indexIsUid
Type: SystemBoolean
If true, messageIndexSet is treated as a sequence of UIDs; otherwise, as a sequence of ordinal message numbers.
flags
Type: SystemString
The string containing IMAP4 flags to be set or reset.
action
Type: MailBee.ImapMailMessageFlagAction
The action to perform with the specified flags (set, reset, etc).
silentMode
Type: SystemBoolean
If true, the server will NOT respond with the new values of flags of the affected messages; otherwise, the server will produce a series of untagged FETCH responses containing the updated flag lists of affected messages.

Return Value

Type: Boolean
true if the message flags have been updated successfully; otherwise, false.
Exceptions
ExceptionCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.
Remarks

To learn how to specify a valid message sequence (messageIndexSet value), see DownloadEnvelopes(String, Boolean) topic.

To clear flag list for certain message(s), call this method passing a null reference or empty string as flags value and Replace as action value.

MailBee ignores the FETCH response returned by the server when silentMode is false. If the application needs to process this data, the developer can use GetServerResponses(String) method to analyze the server response.

Another reason to set silentMode to false is to determine if the server really applied the specified flags by examining the log file (see the sample code below).

Other overloads of SetMessageFlags(String, Boolean, String, MessageFlagAction, Boolean) method set silentMode to true.

Examples

This sample replaces all flags of the last message in the inbox with the non-standard "$Spam" flag. Also demonstrates how to quickly check if the server really applied the specified flags using log file or console output.

Note Note
This sample may throw an exception or do nothing if the mail server forbids using non-standard message flags. Or, the server may set the flag in memory but discard the state change when the current session ends. See the sample in UploadMessage(MailMessage, String, String, String) topic on how to programmatically determine whether the server allows using non-standard flags.
using System;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        // Set where to save log file. However, we do
        // not enable logging here. We'll enable it
        // later for a single moment of setting flags.
        imp.Log.Filename = @"C:\Temp\log.txt";

        // Connect to the server, login and select inbox.
        imp.Connect("imap.somedomain.com");
        imp.Login("jdoe", "secret");
        imp.SelectFolder("INBOX");

        if (imp.MessageCount > 0)
        {
            // Enable logging into a file to track
            // the status of setting flags.
            imp.Log.Enabled = true;

            // Replace all flags of the last message with "$Spam" flag.
            // The developer can then take a look at C:\Temp\log.txt file
            // to find out if untagged "* FETCH (FLAGS)" response to
            // STORE command contains "$Spam" element. If it does, the
            // server indeed applied this flag to the message.
            imp.SetMessageFlags(imp.MessageCount.ToString(), false,
                "$Spam", MessageFlagAction.Replace, false);

            // Disable logging again. Thus, the log file will be pretty
            // small, and the developer will be able to read it easily.
            imp.Log.Enabled = false;

            // Demonstrate alternative way of analyzing FETCH responses.
            string[] flagResponses = imp.GetServerResponses("FETCH");
            foreach (string response in flagResponses)
            {
                Console.WriteLine(response);
            }
        }
        else
        {
            Console.WriteLine("The inbox is empty");
        }

        // Disconnect from the server.
        imp.Disconnect();
    }
}
See Also