MailBee.NET Objects 4.0

Smtp.ErrorOccurred Event

Occurs when the MailBeeException is thrown.

public event ErrorEventHandler ErrorOccurred;

Event Data

The event handler receives an argument of type ErrorEventArgs containing data related to this event. The following ErrorEventArgs properties provide information specific to this event.

Property Description
IsFinalError Indicates whether the exception caused the current event will be thrown by MailBee once the current event handler finished execution.
Reason Gets the exception which caused the current event to be raised.
State Gets a reference to the object which was supplied by the developer in state parameter of asynchronous methods of the mailer components.

Remarks

When MailBee encounters any error, it throws the MailBeeException. However, MailBee can trap this exception if the error was not critical. For instance, if connecting to certain SMTP server fails but another server is available in reserve, the MailBeeException will be thrown and then trapped by MailBee itself, but no exception will be thrown to the application code. Such errors are called warnings.

ErrorOccurred event allows the developer to track both warnings and critical errors because it's raised for a particular exception even if MailBee will trap this exception and do not pass it to the application code.

Example

This sample logs all the warnings and critical errors issued during the SMTP session into the console. In this sample, the connection attempt is made twice (high priority and low priority servers are used). Connection to high-priority SMTP server fails (the server name is invalid) so ErrorOccurred event is raised, but no exception is thrown to the application code. Then, MailBee attempts to connect to the backup server. If this attempt fails too, ErrorOccurred event is raised again (now IsFinalError will be true to indicate the error is not a warning), and the exception is thrown to the application code.

[C#]
using System;
using MailBee;
using MailBee.SmtpMail;

class Sample
{
    // ErrorOccurred event handler.
    private static void OnErrorOccurred(object sender, ErrorEventArgs e)
    {
        // Check whether the error is critical or not.
        if (e.IsFinalError)
        {
            // Display corresponding MailBeeException.Message.
            // After OnErrorOccurred handler finishes, MailBee will 
            // throw the same exception to the application code.
            Console.WriteLine("CRITICAL ERROR: " + e.Reason.Message);
        }
        else
        {
            // Display corresponding MailBeeException.Message.
            // After OnErrorOccurred handler finishes, MailBee will 
            // try another methods to accomplish the current task.
            Console.WriteLine("WARNING: " + e.Reason.Message);
        }
    }

    // The actual code.
    static void Main(string[] args)
    {
        Smtp mailer = new Smtp();

        // Specify top priority server with invalid name.
        mailer.SmtpServers.Add("invalid-host");

        // Specify backup server with valid name.
        mailer.SmtpServers.Add("smtp.domain.com", 25, 1);

        // Subscribe to the ErrorOccurred event.
        mailer.ErrorOccurred += new ErrorEventHandler(OnErrorOccurred);

        // Connect to the servers.
        mailer.Connect();

        Console.WriteLine("Connected to " +
            mailer.SmtpServers[mailer.GetCurrentSmtpServerIndex()].Name);

        // Close the connection.
        mailer.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.SmtpMail

Class Sample
    ' ErrorOccurred event handler.
    Private Shared Sub OnErrorOccurred(ByVal sender As Object, ByVal e As ErrorEventArgs)
        ' Check whether the error is critical or not.
        If e.IsFinalError Then
            ' Display corresponding MailBeeException.Message.
            ' After OnErrorOccurred handler finishes, MailBee will 
            ' throw the same exception to the application code.
            Console.WriteLine("CRITICAL ERROR: " & e.Reason.Message)
        Else
            ' Display corresponding MailBeeException.Message.
            ' After OnErrorOccurred handler finishes, MailBee will 
            ' try another methods to accomplish the current task.
            Console.WriteLine("WARNING: " & e.Reason.Message)
        End If
    End Sub

    Shared Sub Main(ByVal args As String())
        Dim mailer As New Smtp

        ' Specify top priority server with invalid name.
        mailer.SmtpServers.Add("invalid-host")

        ' Specify backup server with valid name.
        mailer.SmtpServers.Add("smtp.domain.com", 25, 1)

        ' Subscribe to the ErrorOccurred event.
        AddHandler mailer.ErrorOccurred, AddressOf OnErrorOccurred

        ' Connect to the servers.
        mailer.Connect()

        Console.WriteLine("Connected to " & _
            mailer.SmtpServers(mailer.GetCurrentSmtpServerIndex()).Name)

        ' Close the connection.
        mailer.Disconnect()
    End Sub
End Class

See Also

Smtp Class | MailBee.SmtpMail Namespace