SmtpTransientErrorOccurred Event |
Namespace: MailBee.SmtpMail
This event may not occur if only a single SMTP server is available for sending to the domain for which sending has failed.
This event lets the application decide what to do when the current SMTP server reports that it cannot deliver your message to the given e-mail address. You can either let MailBee try another server or stop trying the current message. This choice is helpful because the library itself cannot determine if sending should be stopped. Two kinds of transient errors exists:
Unfortunately, there is no distinction between SMTP codes for these errors. Thus, the app or even the user will have to make choice based on their preferences (or you can examine the human-readable response from the server although this is not reliable). If you subscribe to this event, you can control this behavior. If you don't subscribe, MailBee will retry if there is at least one more SMTP relay server left (in case if you're sending via SMTP relay servers) and won't retry in case of direct send.
This default behavior is a general recommendation: you should retry in case of relay SMTP servers (SmtpServers) and don't retry if you're using direct send (MX lookup) so that SmtpServers is empty. SMTP servers obtained via MX lookup often use greylisting so in most cases retrying with another MX will make your IP address getting banned. If, however, your situation is different, you can use this event to adjust the default behavior.
using System; using MailBee; using MailBee.SmtpMail; class Sample { // TransientErrorOccurred event handler. private static void OnTransientErrorOccurred(object sender, SmtpTransientErrorOccurredEventArgs e) { Console.WriteLine("SMTP error code: " + e.Reason.ErrorCode); Console.WriteLine("Server response: " + e.Reason.ResponseString); e.Continue = true; // Enable SMTP MX retries. } // The actual code. static void Main(string[] args) { Smtp mailer = new Smtp(); // Logging is cool for debugging. mailer.Log.Enabled = true; mailer.Log.Filename = @"C:\Temp\log.txt"; mailer.Log.Clear(); // Get DNS servers from the system. mailer.DnsServers.Autodetect(); // Subscribe to the event. mailer.TransientErrorOccurred += new SmtpTransientErrorOccurredEventHandler(OnTransientErrorOccurred); // Prepare and send the message mailer.To.AsString = "john@theirdomain.com"; mailer.From.Email = "alice@yourdomain.com"; mailer.Subject = "Test message"; mailer.Send(); } }