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 |
---|---|
Pop3Exception | The exception that is thrown when an error occurs while using Pop3 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 ComponentPro.Net; using ComponentPro.Net.Mail; ... // POP3 server information. const string serverName = "myserver"; const string user = "name@domain.com"; const string password = "mytestpassword"; const int port = 995; const SslSecurityMode securityMode = SslSecurityMode.Implicit; // Create a new instance of the Pop3 class. Pop3 client = new Pop3(); try { // Connect to the server. client.Connect(serverName, port, securityMode); // Login to the server. client.Authenticate(user, password); StringBuilder sb = new StringBuilder(); Pop3MessageCollection list = client.ListMessages(Pop3EnvelopeParts.Size | Pop3EnvelopeParts.UniqueId); for (int i = 0; i < list.Count; i++) { sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].UniqueId); } Console.WriteLine(sb.ToString()); // Close the connection. client.Disconnect(); } catch (Exception exc) { // Show error. Console.WriteLine("Error: " + exc.Message); // Safely disconnect. if (client.IsConnected) client.Disconnect(); }