ComponentPro UltimateMail

      Setting message flags Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      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

      1. 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;
        
      2. Create a new instance of the Imap class.
      3. 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);
        
      4. 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);
        
      5. 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);
        
      6. 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));
      }