Set, Change & Remove Message Flags on IMAP Server

Set/Change Message Flags

You can change message flags by setting new ones with the ChangeMessageFlags() method. This method takes two parameters.

  1. The sequence number of a message or its unique ID.
  2. MessageFlag.

The following flags can be set:

The following code snippet shows you how to change message flags on an IMAP server with Aspose.Email.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Mark the message as read
client.ChangeMessageFlags(1, ImapMessageFlags.IsRead);

Remove Message Flags

Message flags can also be removed with the RemoveMessageFlags() method. Usage is similar to that of the ChangeMessageFlags() method. It takes a sequence number or unique message ID and MessageFlag. The following code snippet shows you how to remove message flags.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Remove the message flag
client.RemoveMessageFlags(1, ImapMessageFlags.IsRead);

Set Custom Flags

You can also set custom flags to a message using the ImapClient of the API. The ImapClient AddMessageFlags provides the ability to set custom flags on messages.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create a message
MailMessage message = new MailMessage("user@domain1.com", "user@domain2.com", "subject", "message");
//Append the message to mailbox
string uid = client.AppendMessage(ImapFolderInfo.InBox, message);
//Add custom flags to the added messge
client.AddMessageFlags(uid, ImapMessageFlags.Keyword("custom1") | ImapMessageFlags.Keyword("custom1_0"));
//Retreive the messages for checking the presence of custom flag
client.SelectFolder(ImapFolderInfo.InBox);
ImapMessageInfoCollection messageInfos = client.ListMessages();
foreach (var inf in messageInfos)
{
ImapMessageFlags[] flags = inf.Flags.Split();
if (inf.ContainsKeyword("custom1"))
Console.WriteLine("Keyword found");
}