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

Namespace: MailBee.SmtpMail
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 SMTP 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 SMTP 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 SMTP commands to the server, Connected event is raised.

If POP-before-SMTP authentication is used and MailBee successfully connects to the POP3 server host, this event is raised as well. The developer can examine Protocol property value to determine whether the connection was made to POP3 or SMTP server.

Examples
This sample demonstrates the order in which events are raised during Connect method call (POP-before-SMTP authentication is enabled, so certain events first occur for POP3 connection and then for SMTP connection). The events used in this sample occur in the following order:
  1. HostResolved (POP3 server host resolved)
  2. SocketCreating (MailBee is about to create a socket for connecting to POP3 server)
  3. SocketConnected (POP3 server accepted connection)
  4. LowLevelDataReceived (greeting from POP3 server)
  5. DataReceived (greeting from POP3 server)
  6. Connected (connected to POP3 server)
  7. LowLevelDataSent (send user account name to POP3 server)
  8. DataSent
  9. LowLevelDataReceived (POP3 server response to user account name)
  10. DataReceived (POP3 server response to user account name)
  11. LowLevelDataSent (send user account password to POP3 server)
  12. DataSent (send user account password to POP3 server)
  13. LowLevelDataReceived (POP3 greeting to authenticated user)
  14. DataReceived (POP3 greeting to authenticated user)
  15. LowLevelDataSent (send QUIT to logout from POP3 server)
  16. DataSent (send QUIT to logout from POP3 server)
  17. LowLevelDataReceived (POP3 bye message)
  18. DataReceived (empty string indicating the connection is closed)
  19. LowLevelDataReceived (empty string indicating the connection is closed)
  20. DataReceived (POP3 bye message)
  21. Disconnected (disconnected from POP3 server)
  22. HostResolved (SMTP server host resolved)
  23. SocketCreating (MailBee is about to create a socket for connecting to SMTP server)
  24. SocketConnected (SMTP server accepted connection)
  25. LowLevelDataReceived (greeting from SMTP server)
  26. DataReceived (greeting from SMTP server)
  27. Connected (connected to SMTP server)

Once Connect method finishes, all event handlers are removed in the code, thus Disconnect method does not generate any events.

using System;
using MailBee;
using MailBee.SmtpMail;

class Sample
{
    // Replaces CRLF with \r\n in the end of each sent/received string.
    // Used to improve readability of the console output.
    private static string EscapeCrLf(string s)
    {
        return s.Replace("\r\n", @"\r\n");
    }

    // HostResolved event handler (called twice - for POP3 and SMTP). However,
    // since server name is the same for both SMTP and POP3 servers, the IP 
    // address will be the same for both.
    private static void OnHostResolved(object sender, HostResolvedEventArgs e)
    {
        Console.WriteLine("The host name " + e.RemoteHost.HostName +
            " was resolved into IP address(es).");
    }

    // SocketConnected event handler (called twice - for POP3 and SMTP).
    private static void OnSocketConnected(object sender, SocketConnectedEventArgs e)
    {
        string serverType = (e.Protocol == TopLevelProtocolType.Smtp) ? "SMTP" : "POP3";
        Console.WriteLine("The " + serverType + " server at " +
            e.RemoteEndPoint.Address.ToString() + " accepted the connection.");
    }

    // LowLevelDataSent event handler.
    private static void OnLowLevelDataSent(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 LowLevelDataSent event is raised before the 
        // corresponding DataSent event.
        // But in SSL case, DataSent and LowLevelDataSent event data would be 
        // completely different, and some LowLevelDataSent events (such as ones 
        // occurred during the SSL handshake) would not be followed by DataSent 
        // events at all, because the data sent or received during the SSL handshake 
        // does not relate to SMTP protocol and cannot be decoded into textual form.
        Console.WriteLine("Low level data sent: [" +
            EscapeCrLf(System.Text.Encoding.Default.GetString(e.Data)) + "]");
    }

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

    // LowLevelDataReceived event handler.
    private static void OnLowLevelDataReceived(object sender, DataTransferEventArgs e)
    {
        // All considerations for LowLevelDataSent event (see above).
        // also apply to LowLevelDataReceived event.
        Console.WriteLine("Low level data received: [" +
            EscapeCrLf(System.Text.Encoding.Default.GetString(e.Data)) + "]");
    }

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

    // Connected event handler (called twice - for POP3 and SMTP).
    private static void OnConnected(object sender, ConnectedEventArgs e)
    {
        string serverType = (e.Protocol == TopLevelProtocolType.Smtp) ? "SMTP" : "POP3";
        Console.WriteLine("Successfully connected to the " + serverType + " server.");
    }

    // Disconnected event handler (called once - for POP3).
    private static void OnDisconnected(object sender, DisconnectedEventArgs e)
    {
        string serverType = (e.Protocol == TopLevelProtocolType.Smtp) ? "SMTP" : "POP3";
        Console.WriteLine("Successfully disconnected from the " + serverType + " server.");
    }

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

        // Specify SMTP server and enable POP-before-SMTP authentication.
        SmtpServer server = new SmtpServer("mail.company.com");
        server.AuthPopBeforeSmtp = true;
        server.AccountName = "jdoe@company.com"; // or jdoe (depends on mail server).
        server.Password = "secret";
        mailer.SmtpServers.Add(server);

        // Subscribe to the events.
        mailer.HostResolved += new HostResolvedEventHandler(OnHostResolved);
        mailer.SocketConnected += new SocketConnectedEventHandler(OnSocketConnected);
        mailer.LowLevelDataReceived += new DataTransferEventHandler(OnLowLevelDataReceived);
        mailer.DataReceived += new DataTransferEventHandler(OnDataReceived);
        mailer.LowLevelDataSent += new DataTransferEventHandler(OnLowLevelDataSent);
        mailer.DataSent += new DataTransferEventHandler(OnDataSent);
        mailer.Connected += new ConnectedEventHandler(OnConnected);
        mailer.Disconnected += new DisconnectedEventHandler(OnDisconnected);

        // Connect to the server and make the events get raised.
        // Actually, it will connect to POP3 server first, authenticate, disconnect,
        // and immediately connect to SMTP server.
        mailer.Connect();

        // Unsubscribe from the events.
        mailer.HostResolved -= new HostResolvedEventHandler(OnHostResolved);
        mailer.SocketConnected -= new SocketConnectedEventHandler(OnSocketConnected);
        mailer.LowLevelDataReceived -= new DataTransferEventHandler(OnLowLevelDataReceived);
        mailer.DataReceived -= new DataTransferEventHandler(OnDataReceived);
        mailer.LowLevelDataSent -= new DataTransferEventHandler(OnLowLevelDataSent);
        mailer.DataSent -= new DataTransferEventHandler(OnDataSent);
        mailer.Connected -= new ConnectedEventHandler(OnConnected);
        mailer.Disconnected -= new DisconnectedEventHandler(OnDisconnected);

        mailer.Disconnect();
    }
}
See Also