我正在使用c# 4.0构建一个SMTP诊断工具。如果我知道一个域的主NS的IP地址,我可以获得MX、A和CNAME记录。这样我就可以验证任何电子邮件并运行法律诊断命令。,如果我可以连接到邮件服务器。
我的问题是,我不能找到一个适当的.NET解决方案,以获得给定域的主要NS。
我知道有一些托管客户端,但我无法将它们添加为对我的解决方案的引用,或者它们的源代码已关闭。
托管代码和.NET的区别是什么?对于这个问题,托管代码可以查询域名的NS,而.NET不能像声明的那样查询here。?
实现这种功能的正确方式是什么?
问候
发布于 2012-08-16 15:48:32
您可以使用IPInterfaceProperties.DnsAddresses获取您的DNS服务器的IP。可以在以下位置找到代码示例:http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.dnsaddresses.aspx
然后,您可以使用以下位置的组件查询该服务器:http://www.codeproject.com/Articles/12072/C-NET-DNS-query-component
您可以通过查询SOA记录来查找主DNS服务器。
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();https://stackoverflow.com/questions/11981850
复制相似问题