MailBee. NET Objects Tutorials

Exceptions processing

Summary: Demonstrates the use of exception mechanism.

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions. For example:

C#

// Code segment expected to raise the exception
try
{
        pop.Connect(servername);
}
// Code segment contains the exception handler
catch (MailBeeException e)
{
        Console.WriteLine(e.Message);
}

VB.NET

// Code segment expected to raise the exception
try
{
        pop.Connect(servername);
}
// Code segment contains the exception handler
catch (MailBeeException e)
{
        Console.WriteLine(e.Message);
}

The try-block contains guarded code block that may cause an exception. The block is executed until the exception is thrown or it is completed successfully. The catch clause can be used without arguments, in such cases it catches any type of exception, and is referred to as the general catch clause. It can also take an object argument derived from MailBeeException, in this case it handles a specific exception. You can also use exceptions from MailBee exception classes. These are:

Here you can overview MailBeePop3LoginBadCredentialsException methods and properties. The exception that is thrown when the POP3 server reports the given user account name and/or the password is incorrect. The error code returned by MailBeeException.ErrorCode property is the one of the field values defined in ErrorCodes class. You can get a message that describes the current exception by using Message property.

Code example:

The code below demonstrates the use of exception in a simple console application. This code handles two exceptions. The first one can occur during the connection attempt. If the typed in server name does not exist or it is impossible to connect due to a network problem, the program will generate the error message and try to reconnect. The second handled exception is thrown when the POP3 server reports the given user account name and/or the password is incorrect. On success, application prints "From:", "To:" and "Subject:" fields of all messages in the mailbox.

C#

Pop3 pop = new Pop3();
bool b_connected = false;
string server_name = "";
string ch_confirm;

// Connect to POP3 server
do
{
    ch_confirm = "n";
    Console.WriteLine("Enter mail server name:");

    // Get typed in name of sever
    server_name = Console.ReadLine();
    Console.WriteLine("Connecting to " + server_name + "...");

    // Code expected to raise the exception
    try
    {
        pop.Connect(server_name);
        Console.WriteLine("Connection established");
        b_connected = true;
    }

    // Code contains the exception handler
    catch (MailBeeException e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine("Would you like to try again? y/n");
        ch_confirm = Console.ReadLine();
    }

// Try again until connected
} while (b_connected == false && ch_confirm == "y");

if (b_connected == true)
{
    string login;
    string password;
    int i_ErrorCode;
    do
    {
        i_ErrorCode = 0;
        ch_confirm = "n";
        Console.WriteLine("Enter your login: ");

        // Get typed in login
        login = Console.ReadLine();
        Console.WriteLine("Enter your password for POP3 server: ");

        // Get typed in password
        password = Console.ReadLine();
        Console.WriteLine("Loging in to POP3 server. Please wait...");

        // Try to login 
        try
        {
                pop.Login(login, password);
        }

        // Handle bad credentials exception
        catch (MailBeePop3LoginBadCredentialsException e)
        {
            i_ErrorCode = e.ErrorCode;
            Console.WriteLine("Error#" + i_ErrorCode + " " + e.Message);
            Console.WriteLine("Would you like to try again? y/n");
            ch_confirm = Console.ReadLine();
        }

    // Try again until logged in
    } while (ch_confirm == "y");

    if (i_ErrorCode == 0)
    {
        Console.WriteLine("Authentication successful. Receiving headers...");

        // Download headers for all messages
        MailMessageCollection msgs = pop.DownloadMessageHeaders();

        // For each message, write data to the console
        foreach (MailMessage msg in msgs)
        {
            Console.WriteLine("From: " + msg.From.Email + ", To: " + msg.To.AsString);

            Console.WriteLine("Subject: " + msg.Subject);
        }
    }

    // Disconnect from POP3 server
    pop.Disconnect();
}

VB.NET

Dim pop As New Pop3
Dim b_connected As Boolean = False
Dim server_name As String = ""
Dim ch_confirm As String

' Connect to POP3 server
Do
    ch_confirm = "n"
    Console.WriteLine("Enter mail server name:")

    ' Get typed in name of sever
    server_name = Console.ReadLine()
    Console.WriteLine("Connecting to " & server_name & "...")

    ' Code expected to raise the exception
    Try
        pop.Connect(server_name)
        Console.WriteLine("Connection established")
        b_connected = True

       ' Code contains the exception handler
    Catch e As MailBeeException
         Console.WriteLine(e.Message)
        Console.WriteLine("Would you like to try again? y/n")
        ch_confirm = Console.ReadLine()
    End Try

    ' Try again until connected
Loop While b_connected = False And ch_confirm = "y"

If b_connected Then
    Dim login As String
    Dim password As String
    Dim i_ErrorCode As Integer
    Do
        i_ErrorCode = 0
        ch_confirm = "n"
        Console.WriteLine("Enter your login: ")

        ' Get typed in login
        login = Console.ReadLine()
        Console.WriteLine("Enter your password for POP3 server: ")

        ' Get typed in password
        password = Console.ReadLine()
        Console.WriteLine("Loging in to POP3 server. Please wait...")

        ' Try to login 
        Try
            pop.Login(login, password)
            ' Handle bad credentials exception

            Catch e As MailBeePop3LoginBadCredentialsException
                i_ErrorCode = e.ErrorCode
                Console.WriteLine("Error#" & i_ErrorCode & " " & e.Message)
                Console.WriteLine("Would you like to try again? y/n")
                ch_confirm = Console.ReadLine()
            End Try
            ' Try again until logged in
    Loop While ch_confirm = "y"

    If i_ErrorCode = 0 Then
        Console.WriteLine("Authentication successful. Receiving headers...")

        ' Download headers for all messages
        Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders()

        ' For each message, write data to the console
        Dim msg As MailMessage
        For Each msg In msgs
            Console.WriteLine("From: " & msg.From.Email & ", To: " & msg.To.AsString)
            Console.WriteLine("Subject: " & msg.Subject)
        Next
    End If

    ' Disconnect from POP3 server
     pop.Disconnect()
End If