using System;
using System.Net.Sockets;
using System.Text;
using System.Data;
using System.Net;
using System.IO;
using System.Collections;namespace MailTool
{
public class MXRecord
{ public int preference = -1;
public string exchange = null;
public string ip = null; public override string ToString()
{
return "Preference : " + preference + " Exchange : " + exchange +" IP : "+ ip;
} } /// <summary>
/// getMxRecords 的摘要说明。
/// </summary>
public class getMxRecords
{
public getMxRecords()
{
id = DateTime.Now.Millisecond * 60;
dnsServers = new ArrayList();
} private byte[] data;
private int position, id, length;
private string name;
private ArrayList dnsServers; private static int DNS_PORT = 53; Encoding ASCII = Encoding.ASCII; public void setDnsServers(ArrayList dnsServers)
{
this.dnsServers = dnsServers;
} public ArrayList getMXRecords(string host)
{ ArrayList mxRecords = null; for(int i=0; i < dnsServers.Count; i++)
{
try
{
mxRecords = getMXRecords(host,(string)dnsServers[i]);
break;
}
catch(IOException)
{
continue;
}
} return mxRecords;
} private int getNewId()
{ //return a new id
return ++id;
}
using System.Net.Sockets;
using System.Text;
using System.Data;
using System.Net;
using System.IO;
using System.Collections;namespace MailTool
{
public class MXRecord
{ public int preference = -1;
public string exchange = null;
public string ip = null; public override string ToString()
{
return "Preference : " + preference + " Exchange : " + exchange +" IP : "+ ip;
} } /// <summary>
/// getMxRecords 的摘要说明。
/// </summary>
public class getMxRecords
{
public getMxRecords()
{
id = DateTime.Now.Millisecond * 60;
dnsServers = new ArrayList();
} private byte[] data;
private int position, id, length;
private string name;
private ArrayList dnsServers; private static int DNS_PORT = 53; Encoding ASCII = Encoding.ASCII; public void setDnsServers(ArrayList dnsServers)
{
this.dnsServers = dnsServers;
} public ArrayList getMXRecords(string host)
{ ArrayList mxRecords = null; for(int i=0; i < dnsServers.Count; i++)
{
try
{
mxRecords = getMXRecords(host,(string)dnsServers[i]);
break;
}
catch(IOException)
{
continue;
}
} return mxRecords;
} private int getNewId()
{ //return a new id
return ++id;
}
{ //opening the UDP socket at DNS server
//use UDPClient, if you are still with Beta1
UdpClient dnsClient = new UdpClient(serverAddress, DNS_PORT); //preparing the DNS query packet.
makeQuery(getNewId(),host); //send the data packet
dnsClient.Send(data,data.Length); IPEndPoint endpoint = null;
//receive the data packet from DNS server
data = dnsClient.Receive(ref endpoint); length = data.Length; //un pack the byte array & makes an array of MXRecord objects.
return makeResponse(host); } //for packing the information to the format accepted by server
public void makeQuery(int id,String name)
{ data = new byte[512]; for(int i = 0; i < 512; ++i)
{
data[i] = 0;
} data[0] = (byte) (id >> 8);
data[1] = (byte) (id & 0xFF );
data[2] = (byte) 1; data[3] = (byte) 0;
data[4] = (byte) 0; data[5] = (byte) 1;
data[6] = (byte) 0; data[7] = (byte) 0;
data[8] = (byte) 0; data[9] = (byte) 0;
data[10] = (byte) 0; data[11] = (byte) 0; string[] tokens = name.Split(new char[] {'.'});
string label; position = 12; for(int j=0; j<tokens.Length; j++)
{ label = tokens[j];
data[position++] = (byte) (label.Length & 0xFF);
byte[] b = ASCII.GetBytes(label); for(int k=0; k < b.Length; k++)
{
data[position++] = b[k];
} } data[position++] = (byte) 0 ; data[position++] = (byte) 0;
data[position++] = (byte) 15; data[position++] = (byte) 0 ;
data[position++] = (byte) 1 ; } //for un packing the byte array
public ArrayList makeResponse(string host)
{ ArrayList mxRecords = new ArrayList();
MXRecord mxRecord; //NOTE: we are ignoring the unnecessary fields.
// and takes only the data required to build
// MX records. int qCount = ((data[4] & 0xFF) << 8) | (data[5] & 0xFF);
if (qCount < 0)
{
throw new IOException("invalid question count");
} int aCount = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
if (aCount < 0)
{
throw new IOException("invalid answer count");
} position=12; for( int i=0;i<qCount; ++i)
{
name = "";
position = proc(position);
position += 4;
} if(aCount>0)
{
for (int i = 0; i < aCount; ++i)
{ name = "";
position = proc(position); position+=10; int pref = (data[position++] << 8) | (data[position++] & 0xFF); name="";
position = proc(position); mxRecord = new MXRecord();
mxRecord.preference = pref;
mxRecord.exchange = name;
mxRecord.ip = getIP(name);
mxRecords.Add(mxRecord);
}
}
else
{
mxRecord = new MXRecord();
name = host;
mxRecord.preference = aCount;
mxRecord.exchange = name;
mxRecord.ip = getIP(name);
mxRecords.Add(mxRecord);
} return mxRecords;
} private int proc(int position)
{ int len = (data[position++] & 0xFF); if(len == 0)
{
return position;
} int offset; do
{
if ((len & 0xC0) == 0xC0)
{
if (position >= length)
{
return -1;
}
offset = ((len & 0x3F) << 8) | (data[position++] & 0xFF);
proc(offset);
return position;
}
else
{
if ((position + len) > length)
{
return -1;
}
name += ASCII.GetString(data, position, len);
position += len;
} if (position > length)
{
return -1;
} len = data[position++] & 0xFF; if (len != 0)
{
name += ".";
}
}while (len != 0); return position;
} public string getIP(string strHostName)
{
IPAddress ip ;
try
{
IPHostEntry ipInfo = Dns.GetHostByName(strHostName);
IPAddress[] ipAddr = ipInfo.AddressList;
ip = ipAddr[0];
}
catch
{
ip = null;
}
return ip.ToString();
} }
}
=========================================
//添加多个DNS服务器
ArrayList dnsServers = new ArrayList();
dnsServers.Add("158.152.1.58");
dnsServers.Add("202.101.98.55");try
{
getMxRecords objMxRecords = new getMxRecords();
objMxRecords.setDnsServers(dnsServers);
results = objMxRecords.getMXRecords(EmailServ); for(int i=0;i<results.Count;i++)
{
MXRecord mx = (MXRecord)results[i];
listBox1.Items.Add (objValidateBySmtp.Validate(strEmail,mx.exchange.ToString()).ToString() + " "+ mx.exchange.ToString() + " "+ mx.ip.ToString() + " "+ mx.preference.ToString());
break;
}
results = null;
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
怎么回事?
我给出的那个链接也是这段代码
DNS解析: using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;namespace DNS解析
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 152);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(211, 129);
this.richTextBox1.TabIndex = 8;
this.richTextBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(69, 42);
this.button1.TabIndex = 7;
this.button1.Text = "获取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(210, 20);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(136, 23);
this.label1.TabIndex = 5;
this.label1.Text = "请输入主机名或域名:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 128);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 9;
this.label2.Text = "IP地址列表:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 302);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox1,
this.button1,
this.textBox1,
this.label1,
this.label2});
this.Name = "Form1";
this.Text = "DNS解析";
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Text = "";
try
{
IPHostEntry IPHost = Dns.GetHostByName (textBox1.Text);
IPAddress [ ] address = IPHost.AddressList;
//获取IP地址列表
foreach(IPAddress s in address)
{
//显示IP地址
richTextBox1.Text = s.ToString() + "\r\n" + richTextBox1.Text;
}
}
catch
{
MessageBox.Show("指定IP地址的网络主机不在线" ,"警告!");
}
}
}
}