SmtpErrorOccurred Event |
Namespace: MailBee.SmtpMail
When MailBee encounters any error, it throws the MailBeeException. However, MailBee can trap this exception if the error was not critical. For instance, if connecting to certain SMTP server fails but another server is available in reserve, the MailBeeException will be thrown and then trapped by MailBee itself, but no exception will be thrown to the application code. Such errors are called warnings.
ErrorOccurred event allows the developer to track both warnings and critical errors because it's raised for a particular exception even if MailBee will trap this exception and do not pass it to the application code.
using System; using MailBee; using MailBee.SmtpMail; class Sample { // ErrorOccurred event handler. private static void OnErrorOccurred(object sender, ErrorEventArgs e) { // Check whether the error is critical or not. if (e.IsFinalError) { // Display corresponding MailBeeException.Message. // After OnErrorOccurred handler finishes, MailBee will // throw the same exception to the application code. Console.WriteLine("CRITICAL ERROR: " + e.Reason.Message); } else { // Display corresponding MailBeeException.Message. // After OnErrorOccurred handler finishes, MailBee will // try another methods to accomplish the current task. Console.WriteLine("WARNING: " + e.Reason.Message); } } // The actual code. static void Main(string[] args) { Smtp mailer = new Smtp(); // Specify top priority server with invalid name. mailer.SmtpServers.Add("invalid-host"); // Specify backup server with valid name. mailer.SmtpServers.Add("smtp.domain.com", 25, 1); // Subscribe to the ErrorOccurred event. mailer.ErrorOccurred += new ErrorEventHandler(OnErrorOccurred); // Connect to the servers. mailer.Connect(); Console.WriteLine("Connected to " + mailer.SmtpServers[mailer.GetCurrentSmtpServerIndex()].Name); // Close the connection. mailer.Disconnect(); } }