Java DNSLookup MX记录列表。像MXToolBox

Java DNSLookup MX记录列表。像MXToolBox,java,dns,mx-record,Java,Dns,Mx Record,我正在建立一个程序来列出一个域的所有MX记录。一开始它似乎工作得很好,但与在线工具相比之后。存在程序无法获取MX记录的域,而MXToolbox可以 我不确定原因是什么,也不确定需要什么配置 // Print out a sorted list of mail exchange servers for a network domain name import javax.naming.directory.Attribute; import javax.naming.directory.Attrib

我正在建立一个程序来列出一个域的所有MX记录。一开始它似乎工作得很好,但与在线工具相比之后。存在程序无法获取MX记录的域,而MXToolbox可以

我不确定原因是什么,也不确定需要什么配置

// Print out a sorted list of mail exchange servers for a network domain name
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.NamingException;
import java.util.Arrays;
import java.util.Comparator;

public class MailHostsLookup 
{
    public static void main(String args[]) 
    {
        // explain what program does and how to use it 
        if (args.length != 1)
        {
            System.err.println("Print out a sorted list of mail exchange servers ");
            System.err.println("    for a network domain name");
            System.err.println("USAGE: java MailHostsLookup domainName");
            System.exit(-1);
        } 
        try
        {   // print the sorted mail exhchange servers
            for (String mailHost: lookupMailHosts(args[0]))
            {
                System.out.println(mailHost);            
            }
        }
        catch (NamingException e)
        {
            System.err.println("ERROR: No DNS record for '" + args[0] + "'");
            System.exit(-2);
        }
     }

    // returns a String array of mail exchange servers (mail hosts) 
    //     sorted from most preferred to least preferred
    static String[] lookupMailHosts(String domainName) throws NamingException
    {
        // see: RFC 974 - Mail routing and the domain system
        // see: RFC 1034 - Domain names - concepts and facilities
        // see: http://java.sun.com/j2se/1.5.0/docs/guide/jndi/jndi-dns.html
        //    - DNS Service Provider for the Java Naming Directory Interface (JNDI)

        // get the default initial Directory Context
        InitialDirContext iDirC = new InitialDirContext();
        // get the MX records from the default DNS directory service provider
        //    NamingException thrown if no DNS record found for domainName
        Attributes attributes = iDirC.getAttributes("dns:/" + domainName, new String[] {"MX"});
        // attributeMX is an attribute ('list') of the Mail Exchange(MX) Resource Records(RR)
        Attribute attributeMX = attributes.get("MX");

        // if there are no MX RRs then default to domainName (see: RFC 974)
        if (attributeMX == null)
        {
            return (new String[] {domainName});
        }

        // split MX RRs into Preference Values(pvhn[0]) and Host Names(pvhn[1])
        String[][] pvhn = new String[attributeMX.size()][2];
        for (int i = 0; i < attributeMX.size(); i++)
        {
            pvhn[i] = ("" + attributeMX.get(i)).split("\\s+");
        }

        // sort the MX RRs by RR value (lower is preferred)
        Arrays.sort(pvhn, new Comparator<String[]>()
            {
                public int compare(String[] o1, String[] o2)
                {
                    return (Integer.parseInt(o1[0]) - Integer.parseInt(o2[0]));
                }
            });

        // put sorted host names in an array, get rid of any trailing '.' 
        String[] sortedHostNames = new String[pvhn.length];
        for (int i = 0; i < pvhn.length; i++)
        {
            sortedHostNames[i] = pvhn[i][1].endsWith(".") ? 
                pvhn[i][1].substring(0, pvhn[i][1].length() - 1) : pvhn[i][1];
        }
        return sortedHostNames;
    }
}
非常感谢,

这是我的密码

import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;

public class DNSLookup
{
    private InitialDirContext iDirC;

    public DNSLookup ()
    {
         Hashtable<String, String> env = new Hashtable<String, String>();
         //env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
         //env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
         env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.dns.DnsContextFactory");
         //env.put(Context.PROVIDER_URL, "dns://google.com");
         // get the default initial Directory Context
         try {
            iDirC = new InitialDirContext(env);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void lookup (String host, String record)
    {
        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByName(host);
            // show the Internet Address as name/address
            System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());

            // get the DNS records for inetAddress
            Attributes attributes = iDirC.getAttributes("dns:\\"+inetAddress.getHostName());
            // get an enumeration of the attributes and print them out
            //NamingEnumeration<?> attributeEnumeration = attributes.getAll();
/*          while (attributeEnumeration.hasMore())
            {
                System.out.println("" + attributeEnumeration.next());
            }
            attributeEnumeration.close();*/
            Attribute mxRecord = attributes.get(record);
            for (int i=0; i<mxRecord.size();i++)
                System.out.println(mxRecord.get(i));

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        DNSLookup looker = new DNSLookup();
        looker.lookup("truetech.com", "MX");
    }
}
import javax.naming.directory.Attribute;
导入javax.naming.directory.Attributes;
导入javax.naming.directory.InitialDirContext;
导入javax.naming.Context;
导入javax.naming.NamingEnumeration;
导入javax.naming.NamingException;
导入java.net.InetAddress;
导入java.net.UnknownHostException;
导入java.util.Hashtable;
公共类DNSLookup
{
私有初始环境iDirC;
公共DNSLookup()
{
Hashtable env=新的Hashtable();
//put(Context.INITIAL_Context_工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
//env.put(Context.PROVIDER\u URL,“ldap://localhost:389/o=JNDITutorial");
put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.dns.DnsContextFactory”);
//env.put(Context.PROVIDER\u URL,“dns://google.com");
//获取默认的初始目录上下文
试一试{
iDirC=新的InitialDirContext(env);
}捕获(NamingE例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有void查找(字符串主机、字符串记录)
{
InetAddress InetAddress;
试一试{
inetAddress=inetAddress.getByName(主机);
//将Internet地址显示为名称/地址
System.out.println(inetAddress.getHostName()+“”+inetAddress.getHostAddress());
//获取inetAddress的DNS记录
Attributes=iDirC.getAttributes(“dns:\\”+inetAddress.getHostName());
//获取属性的枚举并将其打印出来
//NamingEnumeration attributeEnumeration=attributes.getAll();
/*while(attributenumeration.hasMore())
{
System.out.println(“+attributenumeration.next());
}
attributenumeration.close()*/
Attribute mxRecord=attributes.get(记录);

对于(int i=0;i我知道这是一个迟来的答案(但我希望这对您或其他人有帮助)

关于如何查找MX记录的有效解决方案。该解决方案以IntialDirContext为基础,并继续使用属性定义所需的属性类型

// Print out a sorted list of mail exchange servers for a network domain name
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.NamingException;
import java.util.Arrays;
import java.util.Comparator;

public class MailHostsLookup 
{
    public static void main(String args[]) 
    {
        // explain what program does and how to use it 
        if (args.length != 1)
        {
            System.err.println("Print out a sorted list of mail exchange servers ");
            System.err.println("    for a network domain name");
            System.err.println("USAGE: java MailHostsLookup domainName");
            System.exit(-1);
        } 
        try
        {   // print the sorted mail exhchange servers
            for (String mailHost: lookupMailHosts(args[0]))
            {
                System.out.println(mailHost);            
            }
        }
        catch (NamingException e)
        {
            System.err.println("ERROR: No DNS record for '" + args[0] + "'");
            System.exit(-2);
        }
     }

    // returns a String array of mail exchange servers (mail hosts) 
    //     sorted from most preferred to least preferred
    static String[] lookupMailHosts(String domainName) throws NamingException
    {
        // see: RFC 974 - Mail routing and the domain system
        // see: RFC 1034 - Domain names - concepts and facilities
        // see: http://java.sun.com/j2se/1.5.0/docs/guide/jndi/jndi-dns.html
        //    - DNS Service Provider for the Java Naming Directory Interface (JNDI)

        // get the default initial Directory Context
        InitialDirContext iDirC = new InitialDirContext();
        // get the MX records from the default DNS directory service provider
        //    NamingException thrown if no DNS record found for domainName
        Attributes attributes = iDirC.getAttributes("dns:/" + domainName, new String[] {"MX"});
        // attributeMX is an attribute ('list') of the Mail Exchange(MX) Resource Records(RR)
        Attribute attributeMX = attributes.get("MX");

        // if there are no MX RRs then default to domainName (see: RFC 974)
        if (attributeMX == null)
        {
            return (new String[] {domainName});
        }

        // split MX RRs into Preference Values(pvhn[0]) and Host Names(pvhn[1])
        String[][] pvhn = new String[attributeMX.size()][2];
        for (int i = 0; i < attributeMX.size(); i++)
        {
            pvhn[i] = ("" + attributeMX.get(i)).split("\\s+");
        }

        // sort the MX RRs by RR value (lower is preferred)
        Arrays.sort(pvhn, new Comparator<String[]>()
            {
                public int compare(String[] o1, String[] o2)
                {
                    return (Integer.parseInt(o1[0]) - Integer.parseInt(o2[0]));
                }
            });

        // put sorted host names in an array, get rid of any trailing '.' 
        String[] sortedHostNames = new String[pvhn.length];
        for (int i = 0; i < pvhn.length; i++)
        {
            sortedHostNames[i] = pvhn[i][1].endsWith(".") ? 
                pvhn[i][1].substring(0, pvhn[i][1].length() - 1) : pvhn[i][1];
        }
        return sortedHostNames;
    }
}
//打印网络域名的邮件交换服务器的排序列表
导入javax.naming.directory.Attribute;
导入javax.naming.directory.Attributes;
导入javax.naming.directory.InitialDirContext;
导入javax.naming.NamingException;
导入java.util.array;
导入java.util.Comparator;
公共类MailHostsLookup
{
公共静态void main(字符串参数[])
{
//解释程序的功能和使用方法
如果(args.length!=1)
{
System.err.println(“打印邮件交换服务器的排序列表”);
System.err.println(“用于网络域名”);
System.err.println(“用法:JavaMailHostsLookup域名”);
系统退出(-1);
} 
尝试
{//打印已排序的邮件到更改服务器
对于(字符串mailHost:lookupMailHosts(args[0]))
{
System.out.println(mailHost);
}
}
捕获(NamingE例外)
{
System.err.println(“错误:没有'+args[0]+''的DNS记录”);
系统出口(-2);
}
}
//返回邮件交换服务器(邮件主机)的字符串数组
//从最优先到最不优先排序
静态字符串[]lookupMailHosts(字符串域名)引发NamingException
{
//请参阅:RFC 974-邮件路由和域系统
//参见:RFC1034-域名-概念和设施
//见:http://java.sun.com/j2se/1.5.0/docs/guide/jndi/jndi-dns.html
//-Java命名目录接口(JNDI)的DNS服务提供程序
//获取默认的初始目录上下文
InitialDirContext iDirC=新的InitialDirContext();
//从默认DNS目录服务提供商获取MX记录
//如果未找到域名的DNS记录,则引发NamingException
Attributes Attributes=iDirC.getAttributes(“dns:/”+domainName,新字符串[]{“MX”});
//attributeMX是邮件交换(MX)资源记录(RR)的一个属性(“列表”)
Attribute attributeMX=attributes.get(“MX”);
//如果没有MX RRs,则默认为domainName(请参阅:RFC 974)
if(attributeMX==null)
{
返回(新字符串[]{domainName});
}
//将MX RRs拆分为首选项值(pvhn[0])和主机名(pvhn[1])
String[][]pvhn=新字符串[attributeMX.size()][2];
对于(int i=0;i

有点不幸的是,我的答案是链接到同一个网站…也许有人可以更出色地组织这个空间

u