获取邮箱的DNS和MX 工具类
2019-03-06 07:16:54来源:博客园 阅读 ()
1.导入Maven DNS 包:
<dependency> <groupId>dnsjava</groupId> <artifactId>dnsjava</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>2.0</version> </dependency>
2.DNS工具类:DNSUtil.java
import com.google.common.collect.Lists; import org.xbill.DNS.*; import java.util.ArrayList; import java.util.List; public class DNSUtil { class DNSType{ public static final int MX=1; public static final int A=2; } public static List<String> dnsALookup(final String adr) { List<String> list= Lists.newArrayList(); try { final Lookup lookup = new Lookup(adr, Type.A); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) { if (record instanceof ARecord) { final ARecord ptr = (ARecord) record; list.add(ptr.getAddress().getHostAddress().toString()); } } }catch (Exception e){ e.printStackTrace(); } return list; } public static List<String> mxByDomain(String host){ try { DnsThread dnsThread=new DnsThread(host,DNSType.MX); dnsThread.start(); dnsThread.join(5000); List<String> dnsList=dnsThread.getDnsList(); return dnsList; } catch (Exception e) { return new ArrayList(); } } public static List<String> reverseDNSLookup(final String adr) { List<String> list= Lists.newArrayList(); try { final Lookup lookup = new Lookup(adr, Type.MX); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) { if (record instanceof MXRecord) { final MXRecord ptr = (MXRecord) record; list.add(ptr.getTarget().toString()); } } }catch (Exception e){ e.printStackTrace(); } return list; } public static class DnsThread extends Thread { private List<String> dnsList = new ArrayList(); private String host = ""; private int type=0; DnsThread(String host,int type) { this.host = host; this.type=type; } @Override public void run() { lookup(host); } public void lookup(String host) { try { if(type==DNSType.MX) this.dnsList = reverseDNSLookup(host); if(type==DNSType.A) this.dnsList = dnsALookup(host); } catch (Exception e) { } } public List<String> getDnsList() { return dnsList; } public void setDnsList(List<String> dnsList) { this.dnsList = dnsList; } } public static void main(String[] args) { System.out.println(DNSUtil.dnsALookup("qq.com")); System.out.println(DNSUtil.mxByDomain("qq.com")); } }
2.DNS工具类(改版):DNSUtils.java
package com.lyn.util; import org.xbill.DNS.*; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.InitialDirContext; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; public class DNSUtils{ public static final Long TIMEOUT=3000l; class DNSType{ public static final int MX=1; public static final int A=2; } public static List<String> mxByDomain(String host){ try { long a=System.currentTimeMillis(); DnsThread dnsThread=new DnsThread(host,DNSType.MX); dnsThread.start(); dnsThread.join(TIMEOUT); long b=System.currentTimeMillis(); List<String> dnsList=dnsThread.getDnsList(); System.out.println(host+"耗时"+Double.valueOf((b-a)/1000)+"s"); return dnsList; } catch (Exception e) { return new ArrayList<>(); } } public static List<String> aByDomain(String host){ try { long a=System.currentTimeMillis(); DnsThread dnsThread=new DnsThread(host,DNSType.A); dnsThread.start(); dnsThread.join(TIMEOUT); long b=System.currentTimeMillis(); List<String> dnsList=dnsThread.getDnsList(); // System.out.println(host+"DNS检测耗时"+Double.valueOf((b-a)/1000)+"s"); return dnsList; } catch (Exception e) { return new ArrayList<>(); } } public static List<String> reverseDNSLookup(final String adr) throws Exception { List<String> resultList=searchDNSList(adr); if(resultList.size()>0){ return resultList; } List<String> list= new ArrayList<>(); final Lookup lookup = new Lookup(adr, Type.MX); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) { if (record instanceof MXRecord) { final MXRecord ptr = (MXRecord) record; list.add(ptr.getTarget().toString()); } } return list; } /** * @param address 查询地址 * @return */ public static List<String> searchDNSList(String address) { InitialDirContext context = null; String serverAddr="202.106.0.20"; List<String> resultList = new ArrayList<String>(); try { 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", "dns://" + serverAddr + "/"); env.put("com.sun.jndi.ldap.read.timeout", "5000"); context = new InitialDirContext(env); String dns_attrs[] = { "MX" }; Attributes attrs = context.getAttributes(address, dns_attrs); Attribute attr = attrs.get("MX"); if (attr != null) { for (int i = 0; i < attr.size(); i++) { String str=(String) attr.get(i); if(str.indexOf(" ")!=-1) resultList.add(str.split(" ")[1]); else resultList.add(str); } } } catch (Exception e) { e.printStackTrace(); } finally { if(context!=null){ try { context.close(); } catch (NamingException e) { } } } return resultList; } public static List<String> dnsALookup(final String adr) throws Exception { List<String> list= new ArrayList<>(); final Lookup lookup = new Lookup(adr, Type.A); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) { for (final Record record : records) { if (record instanceof ARecord) { final ARecord ptr = (ARecord) record; list.add(ptr.getAddress().getHostAddress().toString()); } } } return list; } public static class DnsThread extends Thread { private List<String> dnsList = new ArrayList<>(); private String host = ""; private int type=0; DnsThread(String host,int type) { this.host = host; this.type=type; } @Override public void run() { lookup(host); } public void lookup(String host) { try { if(type==DNSType.MX) this.dnsList = reverseDNSLookup(host); if(type==DNSType.A) this.dnsList = dnsALookup(host); } catch (Exception e) { } } public List<String> getDnsList() { return dnsList; } public void setDnsList(List<String> dnsList) { this.dnsList = dnsList; } } public static void main(String[] args){ String[] domain=new String[]{"gmail.com","xinnet.com","wo.cn","138.com"}; for(String ss: domain) { List<String> list = DNSUtils.mxByDomain(ss); System.out.println("得到Mx length:"+list.size()); System.out.println("得到Mx:"+ JSONUtils.obj2json(list)); } // long a=System.currentTimeMillis(); // System.out.println(DNSUtils.mxByDomain("gmail.com")); // long b=System.currentTimeMillis(); // System.out.println(b-a); // String domain=DNSUtils.mxByDomain("gmail.com").get(0); // System.out.println(DNSUtils.mxByDomain("xinnet.com")); // long c=System.currentTimeMillis(); // System.out.println(c-b); // System.out.println(DNSUtils.aByDomain(domain)); // long d=System.currentTimeMillis(); // System.out.println(d-c); // long a=System.currentTimeMillis(); // try { // System.out.println(DNSUtils.reverseDNSLookup("189.cn")); // } catch (Exception e) { // e.printStackTrace(); // } // System.out.println(System.currentTimeMillis()-a); // try { // List ip= DNSUtils.mxByDomain("qq"); // System.out.println(ip); // } catch (Exception e) { // e.printStackTrace(); // } } }
原文链接:https://www.cnblogs.com/yysbolg/p/10482906.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Java中jar包获取资源文件的方式 2020-06-05
- Java连载120-反射机制获取构造方法和父类、父接口 2020-06-05
- Java获取当前时间到凌晨12点剩余秒数 2020-06-03
- class.getResourceAsStream获取结果为NULL idea配置问题 2020-05-31
- SpringMVC中如何获取请求参数?案例详解 2020-05-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash