Jeff Norman
Jeff Norman

Reputation: 1044

What is the best way to validate a email domain?

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#?

Upvotes: 1

Views: 1996

Answers (3)

Andrei Bularca
Andrei Bularca

Reputation: 1024

From command prompt:

nslookup -type=mx <domain.name>

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

Upvotes: 1

rockyashkumar
rockyashkumar

Reputation: 1332

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 ...

Upvotes: 2

Jeremy Massel
Jeremy Massel

Reputation: 2267

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

Upvotes: 2

Related Questions