How to handle system.net.mail.smtpfailedrecipientsexception failed recipients

I want to send email to multiple addresses (more than 1000 users) and use the following code when I use it to send email to less than 100 users who work, but for more than 100 users this does not work and throws failed recipients smtpfailedrecipientsexception! What for? How can I send an email to valid addresses and receive a message about this error?

public void SendMailMessage (string[] to,string message,string subject) { MailMessage mMailMessage = new MailMessage (); int lenght = to.GetLength(0); if (lenght > 1) { foreach (string email in to) { mMailMessage.Bcc.Add ( email ); } } else { mMailMessage.To.Add ( to[0] ); } mMailMessage.From = new MailAddress (" no-replay@mycompany.net "); SmtpClient mSmtpClient = new SmtpClient (); mMailMessage.Body = message; mMailMessage.IsBodyHtml = true; mMailMessage.Priority = MailPriority.Normal; mMailMessage.Subject = subject; mSmtpClient.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = delegate(object s,X509Certificate certificate,X509Chain chain, SslPolicyErrors sslPolicyErrors) {return true;}; try { mSmtpClient.Send (mMailMessage); } catch (SmtpFailedRecipientsException ex){ for (int i = 0; i < ex.InnerExceptions.Length; i++) { SmtpStatusCode status = ex.InnerExceptions[i].StatusCode; if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable) { Logger.Debug("Delivery failed - retrying in 5 seconds."); System.Threading.Thread.Sleep(5000); //client.Send(message); mSmtpClient.Send (mMailMessage); } else { Logger.Debug (string.Format ("Failed to deliver message to {0},{1}", ex.InnerExceptions[i].FailedRecipient, i)); } } } catch (Exception ex) { Logger.Debug (string.Format("Exception caught in RetryIfBusy(): {0}", ex.ToString() )); } } 
+4
source share
1 answer

It looks like the SMTP server you are using rejects emails with more than 100 recipients.

You can:

  • use mailing lists instead of sending messages to each recipient directly
  • send multiple messages to up to 100 recipients
  • talk with the administrator of this server to change the limit

But keep in mind that when you send messages to hundreds of external recipients, spam filters are likely to mark them as spam.

0
source

Source: https://habr.com/ru/post/1479116/


All Articles