Occurs when the connection with the server gets closed.
The event handler receives an argument of type DisconnectedEventArgs containing data related to this event. The following DisconnectedEventArgs properties provide information specific to this event.
| Property | Description |
|---|---|
| IsNormalShutdown | Indicates whether this event occurred as a result of calling Disconnect method so that the proper logout command was issued to the server, or the connection was terminated. |
| Protocol | Gets application-level protocol of the closed connection. |
| RemoteEndPoint | Gets a reference to the end point of the remote host to which the connection was closed. |
| RemoteHostName | Gets the name of the remote host to which the connection was closed. |
| 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 in all the cases when the connection gets closed, including normal shutdown and failures.
If POP-before-SMTP authentication is used and MailBee successfully disconnects from the POP3 server, this event is raised as well. The developer can examine Protocol property value to determine whether POP3 or SMTP connection was closed.
Note If Abort method is called, the connection is immediately closed, but no events (including Disconnected) are raised any longer.
This sample demonstrates that Disconnected event raises even on failures (when MailBeeException is thrown). The sample connects to the SMTP server and then sends invalid data to the server. The server does not respond and the network operation times out.
[C#] using System; using MailBee; using MailBee.SmtpMail; class Sample { // Disconnected event handler. private static void OnDisconnected(object sender, DisconnectedEventArgs e) { if (e.IsNormalShutdown) { // QUIT command was sent to SMTP server. Console.WriteLine("Normally disconnected from the server."); } else { // Rough disconnect (due to failure). Console.WriteLine("The connection was terminated."); } } // The actual code. static void Main(string[] args) { Smtp mailer = new Smtp(); // Specify SMTP server name and timeout value. SmtpServer server = new SmtpServer("smtp.company.com"); // We do not want to wait for MailBeeException too long. // Set 5 seconds as timeout value. // We cannot set it too small because otherwise even Connect() method // would not have a time to connect to the server. Unlike POP3, it's // not possible change timeout setting after the connection has already // been established. server.Timeout = 5000; mailer.SmtpServers.Add(server); // Subscribe to Disconnected event. mailer.Disconnected += new DisconnectedEventHandler(OnDisconnected); // Connect to the server first. mailer.Connect(); // Simulate connection error by sending a request to which the SMTP // server will never respond (due to a lack of "\r\n" in the request data) so // that timeout will occur. // This will cause MailBee to terminate the connection and raise // Disconnected event, and then throw MailBeeException (exceptions are // enabled by default). mailer.ExecuteCustomCommand("NONSENSE"); // This line will probably never execute (unless the server // suddenly responds to NONSENSE sequence of bytes) mailer.Disconnect(); } }
[Visual Basic] Imports System Imports MailBee Imports MailBee.SmtpMail Class Sample ' Connected event handler. Private Shared Sub OnDisconnected(ByVal sender As Object, ByVal e As DisconnectedEventArgs) If e.IsNormalShutdown Then ' QUIT command was sent to SMTP server. Console.WriteLine("Normally disconnected from the server.") Else ' Rough disconnect (due to failure). Console.WriteLine("The connection was terminated.") End If End Sub ' The actual code. Shared Sub Main(ByVal args As String()) Dim mailer As New Smtp ' Specify SMTP server name and timeout value. Dim server As New SmtpServer("smtp.company.com") ' We do not want to wait for MailBeeException too long. ' Set 5 seconds as timeout value. ' We cannot set it too small because otherwise even Connect() method ' would not have a time to connect to the server. Unlike POP3, it's ' not possible change timeout setting after the connection has already ' been established. server.Timeout = 5000 mailer.SmtpServers.Add(server) ' Subscribe to the Disconnected event. AddHandler mailer.Disconnected, AddressOf OnDisconnected ' Connect to the server first. mailer.Connect() ' Simulate connection error by sending a request to which the SMTP ' server will never respond (due to a lack of "\r\n" in the request data) so ' that timeout will occur. ' This will cause MailBee to terminate the connection and raise ' Disconnected event, and then throw MailBeeException (exceptions are ' enabled by default). mailer.ExecuteCustomCommand("NONSENSE") ' This line will probably never execute (unless the server ' suddenly responds to NONSENSE sequence of bytes) mailer.Disconnect() End Sub End Class
Smtp Class | MailBee.SmtpMail Namespace | Disconnect