ComponentPro UltimateMail

      Undeleting messages Synchronously

      Language Filter: AllSend comments on this topic to ComponentPro

      Imap class provides two methods DeleteMessage for deleting messages and UndeleteMessage for undeleting messages.

      You use the DeleteMessage method to mark a message as deleted. It won't appear in subsequent message lists, but will actually only be removed from the mailbox after a call to Purge method, current mailbox is changed using Select or the session is disconnected using Disconnect method. You can recover messages that were marked as deleted by using the UndeleteMessage method.

      The following steps will help you to undelete all messages previously marked as deleted in the current working mailbox using the UndeleteMessage method:

      Undeleting messages

      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 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:
        // IMAP server information. 
        const string serverName = "myserver";
        const string user = "name@domain.com";
        const string password = "mytestpassword";
        const int port = 993;
        const SslSecurityMode securityMode = SslSecurityMode.Implicit;
        
        // Create a new instance of the Imap class.
        Imap client = new Imap();
        
        // 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.
        client.Authenticate(user, password);
        
      5. Now call the UndeleteMessage method to undelete all messages previously marked as deleted in the current working mailbox. The code looks similar to the following:
        // Select 'INBOX' mailbox.
        client.Select("INBOX");
        
        ImapMessageIdCollection set = new ImapMessageIdCollection(1, 2, 3);
        
        // Delete mail messages with sequence numbers 1, 2, and 3.
        client.DeleteMessage(set);
        
        // ... 
         
        // Undelete messages.
        client.UndeleteMessage(set);
        
      6. After completing your work, call the Disconnect method to close the IMAP session.

      Final example code

      using ComponentPro.Net;
      using ComponentPro.Net.Mail;
      
      ...
      
      // IMAP server information. 
      const string serverName = "myserver";
      const string user = "name@domain.com";
      const string password = "mytestpassword";
      const int port = 993;
      const SslSecurityMode securityMode = SslSecurityMode.Implicit;
      
      // Create a new instance of the Imap class.
      Imap client = new Imap();
      
      // Connect to the server.
      client.Connect(serverName, port, securityMode);
      
      // Login to the server.
      client.Authenticate(user, password);
      
      // Select 'INBOX' mailbox.
      client.Select("INBOX");
      
      ImapMessageIdCollection set = new ImapMessageIdCollection(1, 2, 3);
      
      // Delete mail messages with sequence numbers 1, 2, and 3.
      client.DeleteMessage(set);
      
      // ... 
       
      // Undelete messages.
      client.UndeleteMessage(set);
      
      // Close the connection.
      client.Disconnect();