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