SmtpErrorOccurred Event
Occurs when the MailBeeException is thrown.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.4 build 677 for .NET 4.5
public event ErrorEventHandler ErrorOccurred


Type: MailBeeErrorEventHandler

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.

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.
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);
            // 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.

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

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

        // Connect to the servers.

        Console.WriteLine("Connected to " +

        // Close the connection.
See Also