title: Java DNSLookup MX record list. Like MXToolBox
body: <p>I'm building a program to list all MX record of a domain. It seemed to work fine at first, but after comparing to a online tool <a href="http://mxtoolbox.com/" rel="noreferrer">http://mxtoolbox.com/</a>. There are domain that the program can not get MX record while MXToolbox can.</p>
  
  <p>I'm not sure what the reason is or any configuration that required.</p>
  
  <p>Many thanks;</p>
  
  <p>Here is my code.</p>
  
  <pre><code>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&lt;String, String&gt; env = new Hashtable&lt;String, String&gt;();
           //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&lt;?&gt; attributeEnumeration = attributes.getAll();
  /*          while (attributeEnumeration.hasMore())
              {
                  System.out.println("" + attributeEnumeration.next());
              }
              attributeEnumeration.close();*/
              Attribute mxRecord = attributes.get(record);
              for (int i=0; i&lt;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");
      }
  }
  </code></pre>
  
  <p>The above domain will output NullPointer mean no MX record found. While the MXToolBox will output one.</p>