MailBee.NET Objects 4.0

Imap.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 IMAP4 server responds with a FETCH response containing envelope data which could not be completely parsed, 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 IMAP4 session into the console. If the connection succeeds and all envelopes are received without any errors, no output will be produced. The sample is written for a console application.

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

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)
    {
        Imap imp = new Imap();

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

        // Connect to the server, login and select inbox.
        imp.Connect("mail.company.com");
        imp.Login("jdoe@company.com", "secret");
        imp.SelectFolder("INBOX");

        // Download envelopes. If any envelopes could not be completely parsed,
        // this will caused ErrorOccurred event be raised.
        EnvelopeCollection envs = imp.DownloadEnvelopes(Imap.AllMessages, false);

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

Module Sample
    ' ErrorOccurred event handler.
    Private 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.
    Sub Main(ByVal args As String())
        Dim imp As New Imap

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

        ' Connect to the server, login and select inbox.
        imp.Connect("mail.company.com")
        imp.Login("jdoe@company.com", "secret")
        imp.SelectFolder("INBOX")

        ' Download envelopes. If any envelopes could not be completely parsed,
        ' this will caused ErrorOccurred event be raised.
        Dim envs As EnvelopeCollection = imp.DownloadEnvelopes(Imap.AllMessages, False)

        ' Close the connection
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace