C # code to query the MX address of DNS

Source: Internet
Author: User
Tags domain

//c# code to query the MX address of DNS
namespace PAB. Dnsutils
{
 using System;
 using System.Collections;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
 
 public class dnsmx
 {
  public dnsmx ()
  {
 }
 
  [DllImport ("Dnsapi", entrypoint= "Dnsquery_w", CharSet=CharSet.Unicode, Setlasterror=true, exactspelling=true )]
  private static extern int DnsQuery ([MarshalAs (UNMANAGEDTYPE.VBBYREFSTR)]ref string pszname, Querytypes Wtype, queryoptions options, int aipservers, ref IntPtr ppQueryResults, int preserved);
 
  [DllImport ("Dnsapi", CharSet=CharSet.Auto, Setlasterror=true)]
  private static extern void Dnsrecordlistfree (IntPtr precordlist, int FreeType);
 
  public static string[] Getmxrecords (string domain)
  {
   IntPtr ptr1=intptr . Zero;
   IntPtr Ptr2=intptr.zero;

Mxrecord RECMX;

if (Environment.OSVersion.Platform!= platformid.win32nt)
{
throw new NotSupportedException ();
}

ArrayList List1 = new ArrayList ();

int num1 = Dnsmx.dnsquery (ref domain, QUERYTYPES.DNS_TYPE_MX, queryoptions.dns_query_bypass_cache, 0, ref ptr1, 0);

if (num1!= 0) {throw new win32exception (NUM1);}

for (ptr2 = PTR1;!ptr2. Equals (IntPtr.Zero); PTR2 = Recmx.pnext)
{
RECMX = (Mxrecord) marshal.ptrtostructure (Ptr2, typeof (Mxrecord));

if (Recmx.wtype = 15)
{
String Text1 = Marshal.ptrtostringauto (Recmx.pnameexchange);
List1. ADD (Text1);
}
}

Dnsmx.dnsrecordlistfree (PTR2, 0);

Return (string[]) List1. ToArray (typeof (String));
}

Private enum Queryoptions {dns_query_accept_truncated_response = 1, Dns_query_bypass_cache = 8, dns_query_dont_reset_ Ttl_values = 0x100000, Dns_query_no_hosts_file = 0x40, Dns_query_no_local_name = 0x20, DNS_QUERY_NO_NETBT = 0x80, Dns_quer Y_no_recursion = 4, Dns_query_no_wire_query = 0x10, dns_query_reserved = -16777216, Dns_query_return_message = 0x200, DNS_ Query_standard = 0, Dns_query_treat_as_fqdn = 0x1000, dns_query_use_tcp_only = 2, dns_query_wire_only = 0x100}

Private enum Querytypes {dns_type_mx = 15}

[StructLayout (LayoutKind.Sequential)]
private struct Mxrecord
{
Public IntPtr Pnext;
public string pname;
public short wtype;
public short wdatalength;
public int flags;
public int dwttl;
public int dwreserved;
Public IntPtr Pnameexchange;
public short wpreference;
public short Pad;
}
}
}

static void Main (string[] args)
{
String[] s= PAB. DnsUtils.DnsMx.GetMXRecords ("microsoft.com");

foreach (String st in S) Console.WriteLine ("Server: {0}", ST);

Console.ReadLine ();
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.