Occurs when the SMTP server accepts the connection attempt and opens the transmission channel between the remote host (SMTP server) and the client (MailBee).
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. |
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.
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:
Once Connect method finishes, all event handlers are removed in the code, thus Disconnect method does not generate any events.
[C#] 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(); } }
[Visual Basic] Imports System Imports MailBee Imports MailBee.SmtpMail Class Sample Private Shared Function EscapeCrLf(ByVal s As String) Return s.Replace(vbCrLf, "\r\n") End Function ' 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 Shared Sub OnHostResolved(ByVal sender As Object, ByVal e As HostResolvedEventArgs) Console.WriteLine("The host name " & e.RemoteHost.HostName & _ " was resolved into IP address(es).") End Sub ' SocketConnected event handler (called twice - for POP3 and SMTP). Private Shared Sub OnSocketConnected(ByVal sender As Object, ByVal e As SocketConnectedEventArgs) Dim serverType As String If e.Protocol = TopLevelProtocolType.Smtp Then serverType = "SMTP" Else serverType = "POP3" End If Console.WriteLine("The " & serverType & " server at " & _ e.RemoteEndPoint.Address.ToString() & " accepted the connection.") End Sub ' LowLevelDataSent event handler. Private Shared Sub OnLowLevelDataSent(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 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)) & "]") End Sub ' DataSent event handler. Private Shared Sub OnDataSent(ByVal sender As Object, ByVal e As DataTransferEventArgs) Console.WriteLine("Data sent: [" & _ EscapeCrLf(System.Text.Encoding.Default.GetString(e.Data)) & "]") End Sub ' LowLevelDataReceived event handler. Private Shared Sub OnLowLevelDataReceived(ByVal sender As Object, ByVal e As DataTransferEventArgs) ' 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)) & "]") End Sub ' DataReceived event handler. Private Shared Sub OnDataReceived(ByVal sender As Object, ByVal e As DataTransferEventArgs) Console.WriteLine("Data received: [" & _ EscapeCrLf(System.Text.Encoding.Default.GetString(e.Data)) & "]") End Sub ' Connected event handler (called twice - for POP3 and SMTP). Private Shared Sub OnConnected(ByVal sender As Object, ByVal e As ConnectedEventArgs) Dim serverType As String If e.Protocol = TopLevelProtocolType.Smtp Then servertype = "SMTP" Else servertype = "POP3" End If Console.WriteLine("Successfully connected to the " & servertype & " server.") End Sub ' Disconnected event handler (called once - for POP3). Private Shared Sub OnDisconnected(ByVal sender As Object, ByVal e As DisconnectedEventArgs) Dim serverType As String If e.Protocol = TopLevelProtocolType.Smtp Then servertype = "SMTP" Else servertype = "POP3" End If Console.WriteLine("Successfully disconnected from the " & servertype & " server.") End Sub ' The actual code. Shared Sub Main(ByVal args As String()) Dim mailer As New Smtp Dim server As New SmtpServer("mail.company.com") ' Specify SMTP server and enable POP-before-SMTP authentication. 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. AddHandler mailer.HostResolved, AddressOf OnHostResolved AddHandler mailer.SocketConnected, AddressOf OnSocketConnected AddHandler mailer.LowLevelDataReceived, AddressOf OnLowLevelDataReceived AddHandler mailer.DataReceived, AddressOf OnDataReceived AddHandler mailer.LowLevelDataSent, AddressOf OnLowLevelDataSent AddHandler mailer.DataSent, AddressOf OnDataSent AddHandler mailer.Connected, AddressOf OnConnected AddHandler mailer.Disconnected, AddressOf 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. RemoveHandler mailer.HostResolved, AddressOf OnHostResolved RemoveHandler mailer.SocketConnected, AddressOf OnSocketConnected RemoveHandler mailer.LowLevelDataReceived, AddressOf OnLowLevelDataReceived RemoveHandler mailer.DataReceived, AddressOf OnDataReceived RemoveHandler mailer.LowLevelDataSent, AddressOf OnLowLevelDataSent RemoveHandler mailer.DataSent, AddressOf OnDataSent RemoveHandler mailer.Connected, AddressOf OnConnected RemoveHandler mailer.Disconnected, AddressOf OnDisconnected mailer.Disconnect() End Sub End Class
Smtp Class | MailBee.SmtpMail Namespace | Connected