MailBee.NET Objects 4.0

Imap.Disconnected Event

Occurs when the connection with the server gets closed.

public event DisconnectedEventHandler Disconnected;

Event Data

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

Property Description
IsNormalShutdown Indicates whether this event occurred as a result of calling Disconnect method so that the proper logout command was issued to the server, or the connection was terminated.
Protocol Gets application-level protocol of the closed connection.
RemoteEndPoint Gets a reference to the end point of the remote host to which the connection was closed.
RemoteHostName Gets the name of the remote host to which the connection was closed.
State Gets a reference to the object which was supplied by the developer in state parameter of asynchronous methods of the mailer components.

Remarks

This event is raised in all the cases when the connection gets closed, including normal shutdown and failures.

Note   If Abort method is called, the connection is immediately closed, but no events (including Disconnected) are raised any longer.

Example

This sample demonstrates that Disconnected event raises even on failures (when MailBeeException is thrown). The sample set very small timeout value (500 milliseconds), then attempts to connect to the IMAP4 server, log in the account, select Inbox folder, and download the first message. If the server or network connection is slow, the network operation will time out. Otherwise, it will succeed. But in both cases, Disconnected will still be raised.

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

class Sample
{
    // Disconnected event handler.
    private static void OnDisconnected(object sender, DisconnectedEventArgs e)
    {
        if (e.IsNormalShutdown)
        {
            // LOGOUT command was sent to IMAP4 server.
            Console.WriteLine("Normally disconnected from the server.");
        }
        else
        {
            // Rough disconnect (due to failure). This will occur if the server
            // or network connection is quite slow and typical delay exceeds
            // 500 milliseconds timeout value which we set for this sample.
            Console.WriteLine("The connection was terminated.");
        }
    }

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

        // Set relatively small timeout value. Under slow connection,
        // it may cause the connection to be closed due to timeout.
        imp.Timeout = 500;

        // Subscribe to Disconnected event.
        imp.Disconnected += new DisconnectedEventHandler(OnDisconnected);

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

        // Download the first message in the inbox completely. If the connection is
        // slow, this may cause MailBee to terminate the connection and raise 
        // Disconnected event, and then throw MailBeeException (exceptions are 
        // enabled by default).
        MailMessage msg = imp.DownloadEntireMessage(1, false);

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

Module Sample
    ' Disconnected event handler.
    Private Sub OnDisconnected(ByVal sender As Object, ByVal e As DisconnectedEventArgs)
        If (e.IsNormalShutdown) Then
            ' LOGOUT command was sent to IMAP4 server.
            Console.WriteLine("Normally disconnected from the server.")
        Else
            ' Rough disconnect (due to failure). This will occur if the server
            ' or network connection is quite slow and typical delay exceeds
            ' 500 milliseconds timeout value which we set for this sample.
            Console.WriteLine("The connection was terminated.")
        End If
    End Sub

    ' The actual code.
    Sub Main(ByVal args As String())
        Dim imp As New Imap

        ' Set relatively small timeout value. Under slow connection,
        ' it may cause the connection to be closed due to timeout.
        imp.Timeout = 500

        ' Subscribe to Disconnected event.
        AddHandler imp.Disconnected, AddressOf OnDisconnected

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

        ' Download the first message in the inbox completely. If the connection is
        ' slow, this may cause MailBee to terminate the connection and raise 
        ' Disconnected event, and then throw MailBeeException (exceptions are 
        ' enabled by default).
        Dim msg As MailMessage = imp.DownloadEntireMessage(1, False)

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

See Also

Imap Class | MailBee.ImapMail Namespace | Disconnect