MailBee.NET Objects 4.0

Pop3.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 connects to the POP3 server and then sends invalid data to the server. The server does not respond and the network operation times out.

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

class Sample
{
    // Disconnected event handler.
    private static void OnDisconnected(object sender, DisconnectedEventArgs e)
    {
        if (e.IsNormalShutdown)
        {
            // QUIT command was sent to POP3 server.
            Console.WriteLine("Normally disconnected from the server.");
        }
        else
        {
            // Rough disconnect (due to failure). If any messages have been 
            // flagged for deletion, they will possibly not be deleted.
            Console.WriteLine("The connection was terminated.");
        }
    }

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

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

        pop.Connect("mail.domain.com");

        // We do not want to wait too long. Set one second as timeout value.
        pop.Timeout = 1000;

        // Simulate connection error by sending a request to which the POP3
        // server will never respond (due to a lack of "\r\n" in the request data).
        // This will cause MailBee to terminate the connection and raise 
        // Disconnected event, and then throw MailBeeException (exceptions are 
        // enabled by default).
        pop.ExecuteCustomCommand("NONSENSE", false);

        // This line will probably never execute (unless the server 
        // suddenly responds to NONSENSE sequence of bytes).
        pop.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Pop3Mail

Class Sample
    ' Disconnected event handler.
    Private Shared Sub OnDisconnected(ByVal sender As Object, ByVal e As DisconnectedEventArgs)
        If e.IsNormalShutdown Then
            ' QUIT command was sent to POP3 server.
            Console.WriteLine("Normally disconnected from the server.")
        Else
            ' Rough disconnect (due to failure). If any messages have been 
            ' flagged for deletion, they will possibly not be deleted.
            Console.WriteLine("The connection was terminated.")
        End If
    End Sub

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

        ' Subscribe to the LoggedIn event.
        AddHandler pop.Disconnected, AddressOf OnDisconnected

        pop.Connect("mail.domain.com")

        ' We do not want to wait too long. Set one second as timeout value.
        pop.Timeout = 1000

        ' Simulate connection error by sending a request to which the POP3 
        ' server will never respond (due to a lack of "\r\n" in the request data).
        ' This will cause MailBee to terminate the connection and raise 
        ' Disconnected event, and then throw MailBeeException (exceptions are 
        ' enabled by default).
        pop.ExecuteCustomCommand("NONSENSE", False)

        ' This line will probably never execute (unless the server 
        ' suddenly responds to NONSENSE sequence of bytes).
        pop.Disconnect()
    End Sub
End Class

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | Disconnect