Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there any built in method in the .NET library that will return all of the MX records for a given domain? I see how you get CNAMES, but not MX records.

like image 239
Segfault Avatar asked Apr 19 '10 18:04

Segfault


People also ask

How do I find DNS records for DNS?

To check a specific DNS record, you need to specify the nslookup command, an optional record type (for example, A , MX , or TXT ), and the host name that you want to check. Note: If you omit the record type, it defaults to A . The first two lines of output specify the server to which the request was directed.

What information is stored in MX records in the DNS system?

A DNS 'mail exchange' (MX) record directs email to a mail server. The MX record indicates how email messages should be routed in accordance with the Simple Mail Transfer Protocol (SMTP, the standard protocol for all email). Like CNAME records, an MX record must always point to another domain.

What happens if MX record is missing?

If an MX record is missing for the domain, then the mail for the domain will normally be attempted to be delivered to the matching A record. So for the domain “yourdomain.com” if there were no MX records for “yourdomain.com” then the mail would be attempted to delivered to the apex/root record of “yourdomain.com”.


2 Answers

Update 2018/5/23:

Check out MichaC's answer for a newer library that has .NET standard support.

Original Answer:

The ARSoft.Tools.Net library by Alexander Reinert seems to do the job pretty well.

It's available from NuGet:

PM> Install-Package ARSoft.Tools.Net 

Import the namespace:

using ARSoft.Tools.Net.Dns; 

Then making a synchronous lookup is as simple as:

var resolver = new DnsStubResolver(); var records = resolver.Resolve<MxRecord>("gmail.com", RecordType.Mx); foreach (var record in records) {     Console.WriteLine(record.ExchangeDomainName?.ToString()); } 

Which gives us the output:

gmail-smtp-in.l.google.com. alt1.gmail-smtp-in.l.google.com. alt2.gmail-smtp-in.l.google.com. alt3.gmail-smtp-in.l.google.com. alt4.gmail-smtp-in.l.google.com. 

Underneath the hood, it looks like the library constructs the UDP (or TCP) packets necessary to send to the resolver, like you might expect. The library even has logic (invoked with DnsClient.Default) to discover which DNS server to query.

Full documentation can be found here.

like image 71
Michael Kropat Avatar answered Oct 01 '22 13:10

Michael Kropat


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

like image 40
MichaC Avatar answered Oct 01 '22 14:10

MichaC