MailBee.NET Objects 3.1

Pop3.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 the POP3 server responds with a negative reply to an optional POP3 command (such as CAPA), 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 POP3 session into the console. If the connection succeeds and the POP3 server does support CAPA command, no output will be produced. The sample is written for a console application.

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

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)
    {
        Pop3 pop = new Pop3();

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

        // Connect to the server.
        pop.Connect("mail.domain.com");

        // Request any data which needs CAPA command to be issued 
        // in order to obtain the required information. The list of supported 
        // authentication methods can serve this purpose. If CAPA command 
        // fails (but the server responds, i.e. the connection itself is still valid),
        // MailBee will not throw any exception to the application code  
        // (but ErrorOccurred event will still be raised) and will try to issue 
        // AUTH command to get the list of supported authentication methods.
        // If AUTH command fails too, it's still not a critical error. SASL 
        // authentication methods will just not be available.
        AuthenticationMethods authMethods = pop.GetSupportedAuthMethods();

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

Public 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

    ' The actual code
    Shared Sub Main(ByVal args As String())
        Dim pop As New Pop3

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

        ' Connect to the server.
        pop.Connect("mail.domain.com")

        ' Request any data which needs CAPA command to be issued 
        ' in order to obtain the required information. The list of supported 
        ' authentication methods can serve this purpose. If CAPA command 
        ' fails (but the server responds, i.e. the connection itself is still valid),
        ' MailBee will not throw any exception to the application code 
        ' (but ErrorOccurred event will still be raised) and will try to issue 
        ' AUTH command to get the list of supported authentication methods.
        ' If AUTH command fails too, it's still not a critical error. SASL 
        ' authentication methods will just not be available.
        Dim authMethodrs As AuthenticationMethods = pop.GetSupportedAuthMethods()

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

See Also

Pop3 Class | MailBee.Pop3Mail Namespace