MX记录查询(C#)

复制代码
代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Eit.Name.Bcomcn
{
    
class MXSearch
    {
        
public MXSearch()
        {
        }

        
#region importDll

        [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);

        
#endregion


        
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 = MXSearch.DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0ref 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);
                }
            }
            MXSearch.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_QUERY_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;
        }


    }
}
复制代码

 

使用方式:

string[] s= Eit.Name.Bcomcn.MXSearch.GetMXRecords("eit.name");
foreach (string st in s)
Console.WriteLine(
"Server: {0}",st);

 

 

posted @   蛤蟆  阅读(1339)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示