﻿{"id":1266,"date":"2010-08-26T17:08:56","date_gmt":"2010-08-26T09:08:56","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=1266"},"modified":"2010-08-26T17:08:56","modified_gmt":"2010-08-26T09:08:56","slug":"%e4%bd%bf%e7%94%a8c%e5%81%9amx%e8%ae%b0%e5%bd%95%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=1266","title":{"rendered":"\u4f7f\u7528C#\u505aMX\u8bb0\u5f55\u67e5\u8be2"},"content":{"rendered":"<p>[coolcode]<br \/>\nusing System;<br \/>\nusing System.Collections;<br \/>\nusing System.ComponentModel;<br \/>\nusing System.Runtime.InteropServices;<\/p>\n<p>\/\/How to use<br \/>\n\/\/string[] s= MXSearch.GetMXRecords(&#8220;eit.name&#8221;);<br \/>\n\/\/foreach (string st in s)<br \/>\n\/\/Console.WriteLine(&#8220;Server: {0}&#8221;,st);<\/p>\n<p>namespace Eit.Name.Bcomcn<br \/>\n{<br \/>\n    class MXSearch<br \/>\n    {<br \/>\n        public MXSearch()<br \/>\n        {<br \/>\n        }<\/p>\n<p>        #region importDll<\/p>\n<p>        [DllImport(&#8220;dnsapi&#8221;, EntryPoint = &#8220;DnsQuery_W&#8221;, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]<br \/>\n        private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);<\/p>\n<p>        [DllImport(&#8220;dnsapi&#8221;, CharSet = CharSet.Auto, SetLastError = true)]<br \/>\n        private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);<\/p>\n<p>        #endregion<\/p>\n<p>        public static string[] GetMXRecords(string domain)<br \/>\n        {<\/p>\n<p>            IntPtr ptr1 = IntPtr.Zero;<br \/>\n            IntPtr ptr2 = IntPtr.Zero;<br \/>\n            MXRecord recMx;<br \/>\n            if (Environment.OSVersion.Platform != PlatformID.Win32NT)<br \/>\n            {<br \/>\n                throw new NotSupportedException();<br \/>\n            }<br \/>\n            ArrayList list1 = new ArrayList();<br \/>\n            int num1 = MXSearch.DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);<br \/>\n            if (num1 != 0)<br \/>\n            {<br \/>\n                throw new Win32Exception(num1);<br \/>\n            }<br \/>\n            for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)<br \/>\n            {<br \/>\n                recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));<br \/>\n                if (recMx.wType == 15)<br \/>\n                {<br \/>\n                    string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange);<br \/>\n                    list1.Add(text1);<br \/>\n                }<br \/>\n            }<br \/>\n            MXSearch.DnsRecordListFree(ptr2, 0);<br \/>\n            return (string[])list1.ToArray(typeof(string));<br \/>\n        }<\/p>\n<p>        private enum QueryOptions<br \/>\n        {<br \/>\n            DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,<br \/>\n            DNS_QUERY_BYPASS_CACHE = 8,<br \/>\n            DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,<br \/>\n            DNS_QUERY_NO_HOSTS_FILE = 0x40,<br \/>\n            DNS_QUERY_NO_LOCAL_NAME = 0x20,<br \/>\n            DNS_QUERY_NO_NETBT = 0x80,<br \/>\n            DNS_QUERY_NO_RECURSION = 4,<br \/>\n            DNS_QUERY_NO_WIRE_QUERY = 0x10,<br \/>\n            DNS_QUERY_RESERVED = -16777216,<br \/>\n            DNS_QUERY_RETURN_MESSAGE = 0x200,<br \/>\n            DNS_QUERY_STANDARD = 0,<br \/>\n            DNS_QUERY_TREAT_AS_FQDN = 0x1000,<br \/>\n            DNS_QUERY_USE_TCP_ONLY = 2,<br \/>\n            DNS_QUERY_WIRE_ONLY = 0x100<br \/>\n        }<\/p>\n<p>        private enum QueryTypes<br \/>\n        {<br \/>\n            DNS_TYPE_MX = 15<br \/>\n        }<\/p>\n<p>        [StructLayout(LayoutKind.Sequential)]<br \/>\n        private struct MXRecord<br \/>\n        {<br \/>\n            public IntPtr pNext;<br \/>\n            public string pName;<br \/>\n            public short wType;<br \/>\n            public short wDataLength;<br \/>\n            public int flags;<br \/>\n            public int dwTtl;<br \/>\n            public int dwReserved;<br \/>\n            public IntPtr pNameExchange;<br \/>\n            public short wPreference;<br \/>\n            public short Pad;<br \/>\n        }<\/p>\n<p>    }<br \/>\n}<\/p>\n<p>[\/coolcode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[coolcode] using System; using System.Collections; usin [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7],"tags":[24,151,150],"class_list":["post-1266","post","type-post","status-publish","format-standard","hentry","category-csharp","category-fromnetwork","tag-c","tag-dns","tag-mx"],"views":976,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1266"}],"version-history":[{"count":0,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1266\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1266"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}