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

Namespace: MailBee.ImapMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public event SocketConnectedEventHandler SocketConnected

Value

Type: MailBeeSocketConnectedEventHandler
Remarks
This event is raised immediately after Socket successfully connected to the IMAP4 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 IMAP4 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 IMAP4 commands to the server, Connected event is raised.
Examples
This sample demonstrates the order in which events are raised during Connect(String, Int32, Socket, EndPoint) method call. The events used in this sample occur in the following order:
  1. HostResolved
  2. SocketCreating
  3. SocketConnected
  4. LowLevelDataReceived
  5. DataReceived
  6. Connected

Once Connect(String, Int32, Socket, EndPoint) 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(String, Int32, Socket, EndPoint) method does not send any data to the server. However, it would be different if SSL-encrypted connection was used.

using System;
using MailBee;
using MailBee.ImapMail;

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

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

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

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

        imp.Disconnect();
    }
}
See Also