.NET SmtpClient: is there a way to make sure all emails are allowed before sending MailMessage?

I use SmtpClient to send email to multiple recipients. When employees leave the company, their email addresses become invalid. Since their email addresses remain in our database until they are manually deleted, an attempt to send them an email causes our application to throw an exception during SmtpClient.Send(MailMessage) . However, although the exception is thrown, it still sends the email. This is a problem because we want to deal with this error by blocking the user’s attempt to save the record and display a friendly message informing about the removal of any invalid partners from the database.

If there was a way to repeat all the recipes to make sure that they are all valid, we can leave all emails sent until the user satisfies a number of conditions.

+4
source share
2 answers

Its a very old question, I don’t know if you were allowed to solve it.

According to MSDN: http://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.100).aspx

When sending email by sending to multiple recipients, the SMTP server accepts some recipients as valid and rejects others, sends emails to the recipients, and then throws a SmtpFailedRecipientsException . The exception will contain a list of recipients who were rejected.

This is an example of an exception to this exception from MSDN:

 try { client.Send(message); } 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) { Console.WriteLine("Delivery failed - retrying in 5 seconds."); System.Threading.Thread.Sleep(5000); client.Send(message); } else { Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions[i].FailedRecipient); } } } 

Full example here: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpfailedrecipientsexception.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

Internally, Send uses the statuscode returned from the RCPT TO command to raise the corresponding exception.

Check the implementation of PrepareCommand in the RecipientCommand.Send method smtpTransport.SendMail (this method is called inside SmtpClient.Send ). It uses RCPT TO to get the statuscode , which is then parsed into the CheckResponse method, and accordingly a SmtpFailedRecipientsException is SmtpFailedRecipientsException . However, VRFY and RCPT are not very reliable because mail servers tend to delay (throttle NDR) or swallow the response as a measure of protection against spam.

+5
source

Take a look at the following: How can I check if a mailing address exists without sending email?

What you want to do is check if email messages exist before continuing with sending.

Therefore, as indicated in the linked answer, try checking if VRFY or RCPT supported by your company mail server.

Citation:

You can connect to the server and execute the VRFY command. Very few servers support this command, but it is designed for just that. If the server responds with a 2.0.0 DSN, the user exists.

VRFY user

You can issue an RCPT and see if the message is rejected.

MAIL FROM: <>

RCPT TO:

+2
source

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


All Articles