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.
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.
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.
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”.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With