MailBee. NET Objects Tutorials

Errors processing

You can disable the exceptions by setting ThrowExceptions property to false. When the exceptions are disabled and an error occurs, the component methods return false (or a null reference if the method normally returns an object). You can use LastResult, GetErrorDescription, GetServerResponse, GetSocketError members to get more information about the error.

Note: Even if this property is set to false, the exceptions which occur due to the errors in MailBee usage will still be thrown. Typical examples of such errors are: passing invalid arguments to methods, calling methods or settings properties in illegal state (for instance, an attempt to start an asynchronous method while another asynchronous method is not yet finished), etc. Such exceptions indicate programming errors in the application being developed, and thus cannot be suppressed.

C#

Pop3 pop = new Pop3();
bool b_attempt_succeeded = false;
string server_name;

// Sets component not to throw exceptions on errors
pop.ThrowExceptions = false;
string ch_confirm = "y";

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

    // Get printed in name of sever
    server_name = Console.ReadLine();
    Console.WriteLine("Connecting to " + server_name + "...");
    b_attempt_succeeded = pop.Connect(server_name);
    if (!b_attempt_succeeded)
    {
        // Handle error
        Console.WriteLine("Error # " + pop.LastResult + ". Connection not established");
        Console.WriteLine("Would you like to try again? y/n");
        ch_confirm = Console.ReadLine();
    }
    else ch_confirm = "n";
// Try again until connected
}

if (b_attempt_succeeded)
{
    Console.WriteLine("Connection established");
    string login;
    string password;
    b_attempt_succeeded = false;
    ch_confirm = "y";

    while (ch_confirm == "y")
    {
        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 
        b_attempt_succeeded = pop.Login(login, password);

        // Handle bad credentials error
        if (!b_attempt_succeeded)
        {
            Console.WriteLine("Error # " + pop.LastResult + ". Incorrect login or password");
            Console.WriteLine("Would you like to try again? y/n");
            ch_confirm = Console.ReadLine();
        }
        else ch_confirm = "n";
    }// Try again until logged in

    if (b_attempt_succeeded)
    {
        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_attempt_succeeded As Boolean = False
Dim server_name As String

' Sets component not to throw exceptions on errors
pop.ThrowExceptions = False
Dim ch_confirm As String = "y"

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

    ' Get printed in name of sever
    server_name = Console.ReadLine()
    Console.WriteLine("Connecting to " & server_name & "...")
    b_attempt_succeeded = pop.Connect(server_name)

    If Not b_attempt_succeeded Then
        ' Handle error
        Console.WriteLine("Error # " & pop.LastResult & ". Connection not established")
        Console.WriteLine("Would you like to try again? y/n")
        ch_confirm = Console.ReadLine()
    Else
        ch_confirm = "n"
    End If
    ' Try again until connected
End While

If b_attempt_succeeded Then
    Console.WriteLine("Connection established")
    Dim login As String
    Dim password As String
    b_attempt_succeeded = False
    ch_confirm = "y"

    While ch_confirm = "y"
        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 
        b_attempt_succeeded = pop.Login(login, password)

        ' Handle bad credentials error
        If Not b_attempt_succeeded Then
            Console.WriteLine("Error # " & pop.LastResult & ". Incorrect login or password")
            Console.WriteLine("Would you like to try again? y/n")
            ch_confirm = Console.ReadLine()
        Else
            ch_confirm = "n"
        End If
       ' Try again until logged in
    End While

    If b_attempt_succeeded 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

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