MailBee.NET Objects 4.0

Pop3.SocketConnected Event

Occurs when the POP3 server accepts the connection attempt and opens the transmission channel between the remote host (POP3 server) and the client (MailBee).

public event SocketConnectedEventHandler SocketConnected;

Event Data

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

Property Description
Protocol Gets application-level protocol of the established connection.
RemoteEndPoint Gets a reference to the end point of the remote host to which the connection was just established.
RemoteHostName Gets the name of the remote host to which the connection was just established.
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 immediately after Socket successfully connected to the POP3 server host. After the socket itself has been connected and this event raised, the client needs to receive some data from the server in order to complete the procedure of establishing connection with the POP3 server. If the connection must be SSL-encrypted, a few additional round-trips to the server required in order to completely establish the connection. Then, once the connection is fully initialized and ready for sending POP3 commands to the server, Connected event is raised.

Example

This sample demonstrates the order in which events are raised during Connect method call. The events used in this sample occur in the following order:

  1. HostResolved
  2. SocketConnected
  3. LowLevelDataReceived
  4. DataReceived
  5. Connected

Once Connect method finishes, all event handlers are removed in the code, thus Disconnect method does not generate any events (but it would do if we didn't remove LowLevelDataReceived and DataReceived handlers).

DataSent and LowLevelDataSent events are not used in this sample because Connect method does not send any data to the server. However, it would be different if SSL-encrypted connection was used.

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

class Sample
{
    // HostResolved event handler.
    private static void OnHostResolved(object sender, HostResolvedEventArgs e)
    {
        Console.WriteLine("The host name of the server was resolved into IP address.");
    }

    // SocketConnected event handler.
    private static void OnSocketConnected(object sender, SocketConnectedEventArgs e)
    {
        Console.WriteLine("The server accepted the connection.");
    }

    // LowLevelDataReceived event handler.
    private static void OnLowLevelDataReceived(object sender, DataTransferEventArgs e)
    {
        // Since SSL connection is not used, OnDataReceived and OnLowLevelDataReceived 
        // routines will produce the same output. This sample handles both events just 
        // to demonstrate that LowLevelDataReceived event is raised before the 
        // corresponding DataReceived event.
        // But in SSL case, DataReceived and LowLevelDataReceived event data would be 
        // completely different, and some LowLevelDataReceived events (such as ones 
        // occurred during the SSL handshake) would not be followed by DataReceived 
        // events at all, because the data sent or received during the SSL handshake 
        // does not relate to POP3 protocol and cannot be decoded into textual form.
        Console.WriteLine("Low level data received: [" +
            System.Text.Encoding.Default.GetString(e.Data) + "]");
    }

    // DataReceived event handler.
    private static void OnDataReceived(object sender, DataTransferEventArgs e)
    {
        Console.WriteLine("Data received: [" +
            System.Text.Encoding.Default.GetString(e.Data) + "]");
    }

    // Connected event handler.
    private static void OnConnected(object sender, ConnectedEventArgs e)
    {
        Console.WriteLine("Successfully connected to the server.");
    }

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

        // Subscribe to the events.
        pop.HostResolved += new HostResolvedEventHandler(OnHostResolved);
        pop.SocketConnected += new SocketConnectedEventHandler(OnSocketConnected);
        pop.LowLevelDataReceived += new DataTransferEventHandler(OnLowLevelDataReceived);
        pop.DataReceived += new DataTransferEventHandler(OnDataReceived);
        pop.Connected += new ConnectedEventHandler(OnConnected);

        // Connect to the server and make the events get raised.
        pop.Connect("mail.domain.com");

        // Unsubscribe from the events.
        pop.HostResolved -= new HostResolvedEventHandler(OnHostResolved);
        pop.SocketConnected -= new SocketConnectedEventHandler(OnSocketConnected);
        pop.LowLevelDataReceived -= new DataTransferEventHandler(OnLowLevelDataReceived);
        pop.DataReceived -= new DataTransferEventHandler(OnDataReceived);
        pop.Connected -= new ConnectedEventHandler(OnConnected);

        pop.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Pop3Mail

Class Sample
    ' HostResolved event handler.
    Private Shared Sub OnHostResolved(ByVal sender As Object, ByVal e As HostResolvedEventArgs)
        Console.WriteLine("The host name of the server was resolved into IP address.")
    End Sub

    Private Shared Sub OnSocketConnected(ByVal sender As Object, ByVal e As SocketConnectedEventArgs)
        ' SocketConnected event handler.
        Console.WriteLine("The server accepted the connection.")
    End Sub

    ' LowLevelDataReceived event handler.
    Private Shared Sub OnLowLevelDataReceived(ByVal sender As Object, ByVal e As DataTransferEventArgs)
        ' Since SSL connection is not used, OnDataReceived and OnLowLevelDataReceived 
        ' routines will produce the same output. This sample handles both events just 
        ' to demonstrate that LowLevelDataReceived event is raised before the 
        ' corresponding DataReceived event.
        ' But in SSL case, DataReceived and LowLevelDataReceived event data would be 
        ' completely different, and some LowLevelDataReceived events (such as ones 
        ' occurred during the SSL handshake) would not be followed by DataReceived 
        ' events at all, because the data sent or received during the SSL handshake 
        ' does not relate to POP3 protocol and cannot be decoded into textual form.
        Console.WriteLine("Low level data received: [" & _
            System.Text.Encoding.Default.GetString(e.Data) & "]")
    End Sub

    ' DataReceived event handler.
    Private Shared Sub OnDataReceived(ByVal sender As Object, ByVal e As DataTransferEventArgs)
        Console.WriteLine("Data received: [" & _
            System.Text.Encoding.Default.GetString(e.Data) & "]")
    End Sub

    Private Shared Sub OnConnected(ByVal sender As Object, ByVal e As ConnectedEventArgs)
        Console.WriteLine("Successfully connected to the server.")
    End Sub

    ' The actual code.
    Shared Sub Main(ByVal args As String())
        Dim pop As New Pop3
        
        ' Subscribe to the LowLevelDataSent event.
        AddHandler pop.HostResolved, AddressOf OnHostResolved
        AddHandler pop.SocketConnected, AddressOf OnSocketConnected
        AddHandler pop.LowLevelDataReceived, AddressOf OnLowLevelDataReceived
        AddHandler pop.DataReceived, AddressOf OnDataReceived
        AddHandler pop.Connected, AddressOf OnConnected

        ' Connect to the server and make the events get raised.
        pop.Connect("mail.domain.com")

        ' Unsubscribe from the events.
        RemoveHandler pop.HostResolved, AddressOf OnHostResolved
        RemoveHandler pop.SocketConnected, AddressOf OnSocketConnected
        RemoveHandler pop.LowLevelDataReceived, AddressOf OnLowLevelDataReceived
        RemoveHandler pop.DataReceived, AddressOf OnDataReceived
        RemoveHandler pop.Connected, AddressOf OnConnected

        pop.Disconnect()
    End Sub
 End Class

See Also

Pop3 Class | MailBee.Pop3Mail Namespace | Connected