How do I know if a domain is harmful before calling SmtpClient.Send ()?

I have a code that sends letters to users from the registration web page. Sometimes users mistakenly use an email address, and the address ends up with a bad domain.

I would really like to check if the domain is corrupted before you call the System.Net.Mm.SmtpClient.Send () method, as this will lead to a better user experience. I think using AJAX to immediately tell the user that their email will not be sent without having to programmatically send an email before figuring out that this is bad, much more elegant.

I am currently handling the error just fine, so please do not discuss how to handle the errors. The error I get is: "The mailbox is unavailable. Server response was :: recipient address rejected: domain not found"

Does anyone know how to use the same (or exact) code to check if a domain is bad or not before calling the Send () method?

+3
source share
4 answers

The most reliable way to ensure that the mail server is indeed configured for this domain is to request an MX record for this domain.

+3
source

Try something like this:

string email = "person@domain.com"
MailAddress ma = new MailAddress(email); // Throws exception if email address is incorrectly formatted
System.Net.Dns.GetHostEntry(ma.Host); // Throws exception if host is invalid
+2
source

, System.Net.Dns.GetHostByName(hostname) , - ?

MSDN , ...

, System.Net.Dns.GetHostEntry(hostname) ?

0

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


All Articles