日期:2014-05-17 浏览次数:20921 次
String dns = "dns://"; //得到接受方的服务器域名 String domain = mesg.getTo().substring(mesg.getTo().indexOf('@') + 1); System.out.println("执行:得到域名:"+domain); Hashtable env = new Hashtable(); System.out.println("执行2"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); System.out.println("执行3"); env.put(Context.PROVIDER_URL, dns); System.out.println("执行4"); DirContext ctx = new InitialDirContext(env); System.out.println("执行5"); Attributes attrs =null; System.out.println("执行6"); try{ System.out.println("执行7"); attrs = ctx.getAttributes(domain, new String[] {"MX"}); System.out.println("执行8"); }catch(NamingException e){ e.printStackTrace(); }catch(Exception e){ System.out.println("执行9"); e.printStackTrace(); } System.out.println("执行:attrs:"+attrs+"//attrs.size()//"+attrs.size()); if (attrs == null || attrs.size() <= 0) { System.out.println("执行6"); throw new java.lang.IllegalStateException("错误:你的DNS服务器没有相应的MX记录!"); } NamingEnumeration servers = attrs.getAll(); // 列出所有邮件服务器: String smtpHost = null; boolean isSend = false; StringBuffer buf = new StringBuffer(); while (servers.hasMore()) { Attribute hosts = (Attribute) servers.next(); for (int i = 0; i < hosts.size(); ++i) { smtpHost = (String) hosts.get(i); smtpHost = smtpHost.substring(smtpHost.lastIndexOf(' ') + 1); try{ System.out.println("执行:smtpHost:"+smtpHost+"//mesg//"+mesg); sendEms(smtpHost, mesg); isSend = true; return; }catch(Exception e){ e.printStackTrace(); buf.append(e.toString()).append("\r\n"); continue; } } } System.out.println("执行:isSend:"+isSend); if (!isSend) { throw new java.lang.IllegalStateException("错误:发送失败," + buf.toString()); }