c# - how to handle system.net.mail.smtpfailedrecipientsexception failed recipients -
i wanna send email multiple addresses (more 1000 users) , use following code, when use send email less 100 users works, more 100 users not work , throw smtpfailedrecipientsexception failed recipients! why? how can send email valid addresses , ride of 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){ (int = 0; < 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 deliver message {0},{1}", ex.innerexceptions[i].failedrecipient, i)); } } } catch (exception ex) { logger.debug (string.format("exception caught in retryifbusy(): {0}", ex.tostring() )); } }
looks smtp server using rejecting mails have more 100 recepients.
you can:
- use distribution lists instead of sending message each recipient directly
- send multiple messages no more 100 recipients
- talk admin of server change limit
but aware, when sending messages hundred of external recipients, spam filters flag them spam.
Comments
Post a Comment