SmtpSocketConnected Event |
Namespace: MailBee.SmtpMail
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.
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(); } }