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

前端 未结 7 1294
难免孤独
难免孤独 2020-11-29 00:34

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.

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 01:15

    The accepted answer doesn't work for .NET framework < 4.5, so would suggest that those of you who can't use ARSOFT.Tools can use DNDNs from https://dndns.codeplex.com

    Given below is a console application that returns the MX record for a given domain modifying their examples.

    using System;
    using System.Net.Sockets;
    using DnDns.Enums;
    using DnDns.Query;
    using DnDns.Records;
    
    namespace DnDnsExamples
    {
    class Program
    {
        static void Main(string[] args)
        {
            DnsQueryRequest request3 = new DnsQueryRequest();
            DnsQueryResponse response3 = request3.Resolve("gmail.com", NsType.MX, NsClass.INET, ProtocolType.Tcp);
            OutputResults(response3);
            Console.ReadLine();
        }
    
        private static void OutputResults(DnsQueryResponse response)
        {
            foreach (IDnsRecord record in response.Answers)
            {
                Console.WriteLine(record.Answer);
                Console.WriteLine("  |--- RDATA Field Length: " + record.DnsHeader.DataLength);
                Console.WriteLine("  |--- Name: " + record.DnsHeader.Name);
                Console.WriteLine("  |--- NS Class: " + record.DnsHeader.NsClass);
                Console.WriteLine("  |--- NS Type: " + record.DnsHeader.NsType);
                Console.WriteLine("  |--- TTL: " + record.DnsHeader.TimeToLive);
                Console.WriteLine();
            }            
        }
    }
    }
    

提交回复
热议问题