1

In my application I validate a email domain like this:

public bool DomainValid(string domainName)
{
    try
    {
        IPHostEntry entry = Dns.GetHostEntry(domainName);
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

The method is good, but not on every cases, like 'mpg.ro' is a valid email domain but it catches an Exception.

Can someone give me another idea of email domain validation in C#?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Jeff Norman
  • 1,014
  • 5
  • 25
  • 42

3 Answers3

2

You could request the root of the domain, then parse the HTTP response? If it's 200, you're good to go

Jeremy Massel
  • 2,257
  • 18
  • 22
  • can you give me an example? Thanks – Jeff Norman Nov 09 '11 at 15:30
  • check out: http://stackoverflow.com/questions/1330856/getting-http-status-code-number-200-301-404-etc-from-httpwebrequest-and-ht You'll definitely want the exception handling recommended there too. – Jeremy Massel Nov 09 '11 at 15:32
  • yes but... HttpWebRequest expects a web page and 'mpg.ro' (for example) is just a domain, there is no web page www.mpg.ro, so it enters an exception there too.. – Jeff Norman Nov 09 '11 at 15:43
2

You could do a dns lookup on the mx record. Here's an example at Code Project:
http://www.codeproject.com/KB/IP/dnslookupdotnet.aspx

I hope it will helps you ...

rockyashkumar
  • 1,302
  • 4
  • 13
  • 24
1

From command prompt:

nslookup -type=mx <domain.name>

you can implement this inside C# too and read the answer.

Andrei Bularca
  • 914
  • 2
  • 11
  • 29