IMAP defines a number of message flags such as "Answered", "Deleted", "Draft", "Seen". Most of them can be changed. Only the "Recent" flag is read-only, the flag indicates that the message has recently arrived and it is the first and only session notified about the message. You can use the response returned from the ListMessages method to get the message flags.
The following steps will help you to update flags of a message that:
Update flags of a message
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using System;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
- Create a new instance of the Imap class.
- Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "password";
const int port = 993;
const string folder = "Inbox";
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
Imap client = new Imap();
try
{
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port);
// Connect to the server.
client.Connect(serverName, port, securityMode);
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "password"
Const port As Integer = 993
Const folder As String = "Inbox"
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
Dim client As New Imap()
Try
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port)
' Connect to the server.
client.Connect(serverName, port, securityMode)
- Use your user name and password to login with Authenticate methods. The code looks similar to the following:
// Login to the server.
Console.WriteLine("Logging in as {0}...", user);
client.Authenticate(user, password);
// Select working folder.
Console.WriteLine("Selecting folder '{0}'...", folder);
client.Select(folder);
' Login to the server.
Console.WriteLine("Logging in as {0}...", user)
client.Authenticate(user, password)
' Select working folder.
Console.WriteLine("Selecting folder '{0}'...", folder)
client.Select(folder)
- Now select a mailbox and pass the sequence number of the message to update, update action, and message flags to the Flag method. The code looks similar to the following:
// Set the first message in the Inbox as Read.
Console.WriteLine("Selecting the first message as Read...");
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Seen);
// Set the first message in the Inbox as Deleted.
// Setting the "Deleted" flag is equivalent to client.Delete method.
Console.WriteLine("Selecting the first message as Deleted...");
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Deleted);
' Set the first message in the Inbox as Read.
Console.WriteLine("Selecting the first message as Read...")
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Seen)
' Set the first message in the Inbox as Deleted.
' Setting the "Deleted" flag is equivalent to client.Delete method.
Console.WriteLine("Selecting the first message as Deleted...")
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Deleted)
- After completing your work, call the Disconnect method to close the IMAP session.
Final example code
using System;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "password";
const int port = 993;
const string folder = "Inbox";
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
Imap client = new Imap();
try
{
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port);
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
Console.WriteLine("Logging in as {0}...", user);
client.Authenticate(user, password);
// Select working folder.
Console.WriteLine("Selecting folder '{0}'...", folder);
client.Select(folder);
// Set the first message in the Inbox as Read.
Console.WriteLine("Selecting the first message as Read...");
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Seen);
// Set the first message in the Inbox as Deleted.
// Setting the "Deleted" flag is equivalent to client.Delete method.
Console.WriteLine("Selecting the first message as Deleted...");
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Deleted);
// Disconnect.
Console.WriteLine("Disconnecting...");
client.Disconnect();
}
catch (ImapException imapExc)
{
Console.WriteLine(string.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status));
}
catch (Exception exc)
{
Console.WriteLine(string.Format("An error occurred: {0}", exc.Message));
}
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "password"
Const port As Integer = 993
Const folder As String = "Inbox"
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
Dim client As New Imap()
Try
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port)
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
Console.WriteLine("Logging in as {0}...", user)
client.Authenticate(user, password)
' Select working folder.
Console.WriteLine("Selecting folder '{0}'...", folder)
client.Select(folder)
' Set the first message in the Inbox as Read.
Console.WriteLine("Selecting the first message as Read...")
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Seen)
' Set the first message in the Inbox as Deleted.
' Setting the "Deleted" flag is equivalent to client.Delete method.
Console.WriteLine("Selecting the first message as Deleted...")
client.Flag(1, ImapFlagModifier.Add, ImapMessageFlags.Deleted)
' Disconnect.
Console.WriteLine("Disconnecting...")
client.Disconnect()
Catch imapExc As ImapException
Console.WriteLine(String.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status))
Catch exc As Exception
Console.WriteLine(String.Format("An error occurred: {0}", exc.Message))
End Try