Message Flags

One of the advantages over the POP protocol is the use of message flags. Message flags are used to define various message states, such as if a message has been read or not.

The example shows how to read, modify, and delete standard and custom message flags using the GemBox.Email component.

Screenshot
Message Flags Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Imap;
using GemBox.Email.Mime;
using GemBox.Email.Security;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            imap.Connect();
            Console.WriteLine("Connected.");

            imap.Authenticate("<USERNAME>", "<PASSWORD>");
            Console.WriteLine("Authenticated.");

            // For this example we will select INBOX folder
            imap.SelectInbox();

            // Get first message flags
            IList<string> flags = imap.GetMessageFlags(1);

            Console.WriteLine("Listing message flags...");

            foreach (string flag in flags)
                Console.WriteLine(' ' + flag);

            // Add 'Draft' flag to message
            imap.AddMessageFlags(1, ImapMessageFlags.Draft);

            // List message flags again
            Console.WriteLine("Listing message flags again...");

            foreach (string flag in imap.GetMessageFlags(1))
                Console.WriteLine(' ' + flag);

            // Remove 'Draft' flag
            imap.RemoveMessageFlags(1, ImapMessageFlags.Draft);

            // Final message listing
            Console.WriteLine("Final message flags listing...");

            foreach (string flag in imap.GetMessageFlags(1))
                Console.WriteLine(' ' + flag);
        }
    }
}
Imports System.Globalization
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports GemBox.Email
Imports GemBox.Email.Imap
Imports GemBox.Email.Mime

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")

            imap.Connect()
            Console.WriteLine("Connected.")

            imap.Authenticate("<USERNAME>", "<PASSWORD>")
            Console.WriteLine("Authenticated.")

            ' For this example we will select INBOX folder
            imap.SelectInbox()

            ' Get first message flags
            Dim flags As IList(Of String) = imap.GetMessageFlags(1)

            Console.WriteLine("Listing message flags...")

            For Each flag As String In flags
                Console.WriteLine(" "c & flag)
            Next

            ' Add 'Draft' flag to message
            imap.AddMessageFlags(1, ImapMessageFlags.Draft)

            ' List message flags again
            Console.WriteLine("Listing message flags again...")

            For Each flag As String In imap.GetMessageFlags(1)
                Console.WriteLine(" "c & flag)
            Next

            ' Remove 'Draft' flag
            imap.RemoveMessageFlags(1, ImapMessageFlags.Draft)

            ' Final message listing
            Console.WriteLine("Final message flags listing...")

            For Each flag As String In imap.GetMessageFlags(1)
                Console.WriteLine(" "c & flag)
            Next

        End Using

    End Sub

End Module