ComponentPro UltimateMail

      Handling exceptions

      Language Filter: AllSend comments on this topic to ComponentPro

      Handling Exceptions

      Exceptions thrown by Mail component can be easily caught by using try catch block. The table below lists all exceptions relating to the Mail component: 

      Exception Description
      ImapException The exception that is thrown when an error occurs while using Imap classes.
      MailException The exception that is thrown when an e-mail related error occurs.
      ProxySocketException The exception that is thrown when a proxy error or socket error occurs.

      The example below shows how to handle exceptions:

      using System;
      using System.Text;
      using System.Windows.Forms;
      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();
      
      try
      {
          // Connect to the server. 
          client.Connect(serverName, port, securityMode);
      
          // Login to the server. 
          client.Authenticate(user, password);
      
          StringBuilder sb = new StringBuilder();
      
          FolderCollection list = client.ListFolders();
          for (int i = 0; i < list.Count; i++)
          {
              sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
          }
      
          MessageBox.Show(sb.ToString());
      
          // Close the connection. 
          client.Disconnect();
      }
      catch (Exception exc)
      {
          // Show error. 
          Console.WriteLine("Error: " + exc.Message);
      
          // Safely disconnect. 
          if (client.IsConnected)
              client.Disconnect();
      }