import java.net.InetSocketAddress;
import java.util.Hashtable;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
/**
* 使用JNDI的一个简单示例<br>
* 该示例向一个DNS服务器查询SMTP服务器列表
* @author 88250
* @version 1.0.0, 2007-8-19
*/
public class MXTest
{
private static final String dnsName = "dns://61.166.150.101";
private static final String domainName =
new InetSocketAddress("61.166.150.101",8080).getHostName();
/**
* 程序入口点
* @param args 应该为<code>null</code>
* @exception NamingException
*/
public static void main(String[] args)
throws NamingException
{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", dnsName);
// 创建环境对象
DirContext dirContext = new InitialDirContext(env);
// 读取环境对象的属性
Attributes attrs = dirContext.getAttributes(domainName, new String[] {"MX"});
for (NamingEnumeration ae = attrs.getAll();
ae != null && ae.hasMoreElements(); )
{
Attribute attr = (Attribute)ae.next();
NamingEnumeration e = attr.getAll();
while (e.hasMoreElements())
{
String element = e.nextElement().toString();
System.out.println(element);
}
}
}
}
import java.util.Hashtable;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
/**
* 使用JNDI的一个简单示例<br>
* 该示例向一个DNS服务器查询SMTP服务器列表
* @author 88250
* @version 1.0.0, 2007-8-19
*/
public class MXTest
{
private static final String dnsName = "dns://61.166.150.101";
private static final String domainName =
new InetSocketAddress("61.166.150.101",8080).getHostName();
/**
* 程序入口点
* @param args 应该为<code>null</code>
* @exception NamingException
*/
public static void main(String[] args)
throws NamingException
{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", dnsName);
// 创建环境对象
DirContext dirContext = new InitialDirContext(env);
// 读取环境对象的属性
Attributes attrs = dirContext.getAttributes(domainName, new String[] {"MX"});
for (NamingEnumeration ae = attrs.getAll();
ae != null && ae.hasMoreElements(); )
{
Attribute attr = (Attribute)ae.next();
NamingEnumeration e = attr.getAll();
while (e.hasMoreElements())
{
String element = e.nextElement().toString();
System.out.println(element);
}
}
}
}