How to Get Mx Records for a Dns Name with System.Net.Dns

How to get mx records for a dns name with System.Net.DNS?

Just roled my own library because there was nothing for .net core / xplat support... https://github.com/MichaCo/DnsClient.NET

It works pretty great and gives you dig like log messages if you want.

Simple to use

var lookup = new LookupClient();
var result = await lookup.QueryAsync("google.com", QueryType.ANY);

and works with custom servers running on any ports, multiple servers, etc...

see also DnsClient Website for more details

How to lookup mx records in dot net core?

I ended up creating my own library to do this as there was no other supporting .net-core.

Try DnsClient.NET https://github.com/MichaCo/DnsClient.NET.

Pretty simple to use:

var lookup = new LookupClient();
var result = await lookup.QueryAsync("google.com", QueryType.ANY);

var record = result.Answers.ARecords().FirstOrDefault();
var address = record?.Address;

You can also specify a DNS Server explicitly if you want.

Support for advanced record types or DNSSEC is not done yet though.

Also, maybe one day the .NET library will have support for that, too. I'm working on a API draft. But till then, you have to use some library or write a ton of code ;)

Get all DNS records

It is not really easy without any library.

You can use native code though, DnsQueryEx is a good starting point...
You'll need tons of code to get it working in C# though.

Why not just use an existing library?

I just wrote one for donet core /xplat support for example:
https://github.com/MichaCo/DnsClient.NET

which is really straight forward to use

var lookup = new LookupClient();
var result = await lookup.QueryAsync("google.com", QueryType.ANY);

var record = result.Answers.ARecords().FirstOrDefault();
var address = record?.Address;

See also http://dnsclient.michaco.net for more details/docs

Finding the MX Record using C#?

You can use the answer of Robert and RPK to get the MX record of a given domain.

But you'll need a DNS server to do the job. If you want to detect the DNS server of the machine where your code is executed, you can use the following.

NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties properties = adapter.GetIPProperties();

if (properties.DnsAddresses.Count > 0)
foreach (IPAddress ipAddress in properties.DnsAddresses)
dnsServers.Add(ipAddress.ToString(), 53);
}

There is a complete solution (or at github here) that will do the whole job if you don't want to rewrite everything. Look for GetMxRecords static method.

MX records of a remote domain without NS information

You can get the IP of your DNS server using IPInterfaceProperties.DnsAddresses. A code example can be found here: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.dnsaddresses.aspx

You can then query that server using the component found here: http://www.codeproject.com/Articles/12072/C-NET-DNS-query-component

You can find the primary DNS server by querying for SOA records.

List<IPAddress> dnsServers = new List<IPAddress>();

NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
IPAddressCollection adapterDnsServers = adapterProperties.DnsAddresses;

if (adapterDnsServers.Count > 0)
dnsServers.AddRange(adapterDnsServers);
}

foreach (IPAddress dnsServer in (from d in dnsServers
where d.AddressFamily == AddressFamily.InterNetwork
select d))
{
Console.WriteLine("Using server {0}", dnsServer);

// create a request
Request request = new Request();

// add the question
request.AddQuestion(new Question("stackoverflow.com", DnsType.MX, DnsClass.IN));

// send the query and collect the response
Response response = Resolver.Lookup(request, dnsServer);

// iterate through all the answers and display the output
foreach (Answer answer in response.Answers)
{
MXRecord record = (MXRecord)answer.Record;
Console.WriteLine("{0} ({1}), preference {2}", record.DomainName, Dns.GetHostAddresses(record.DomainName)[0], record.Preference);
}

Console.WriteLine();
}

Console.ReadLine();

c#: get CNAME & MX record from domain?

I haven't tried the libraries mentioned above, but the I would consider using services instead of libraries. I don't know what's the desired use-case, but you can deliver this functionality even on asp.net web application, cross-platform mobile application or mobile website.

  1. Look for online DNS tools (such as DNSWatch), which returns the necessary domain info by URL parameters.
  2. Check the HTML stucture of the result page

You can process this HTML, but Yahoo can do is dirty job for you using YQL html processor and return the relevant part in XML or JSON.

  1. Visit YQL Console
  2. Fill the SQL Statement textarea with the following:
    select * from html where url="YOUR PARAMETRIC DNS URL" and
    xpath='XPATH QUERY TO ACCESS NS OR MX RECORD IN THE RESULT HTML'
  3. Test your YQL Statement in the console
  4. Develop a single client application, which executes the YQL statement via HTTP request. You can take hints from this YQL JavaScript tutorial.

Pros:
-your application can be very lightweight
Cons:
-the HTML structure of the result page can be changed or DNSWatch can refuse your request after over-using resources.

Nevertheless you can implement the working prototype in 1 hour.



Related Topics



Leave a reply



Submit