Skip to content
Advertisement

Java failure in looking up the MX record for specific domain

Here’s a piece of code demonstrating a case where the Java standard extensions for working with DNS fail to correctly look up the MX record for a specific domain, while the dig utility has no problem doing so. I have replicated this across different machines, networks, and operating systems (AWS Ubuntu, Linode CentOS, local Windows).

import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.NamingException;

public class Main
{
    public static void main(String[] args) throws NamingException {
        String domain = "designercakesbyapril.com";
        InitialDirContext ctx = new InitialDirContext();
        Attributes attributes = ctx.getAttributes("dns:/" + domain, new String[]{"MX"});
        System.out.println(attributes);
        
        // outputs "No attributes"
    }
}

Meanwhile, here’s dig:

dig +short MX designercakesbyapril.com
traff-1.hugedomains.com.
hdr-nlb9-41371129e8304c29.elb.us-east-1.amazonaws.com.

What could be the problem? I’m interested in reliably looking up MX records from Java.

Environment:

openjdk 11.0.14.1 2022-02-08 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.14.1+1-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.14.1+1-LTS, mixed mode, sharing)

Advertisement

Answer

designercakesbyapril.com is not configured correctly.

Requests reply with a CNAME record which is invalid there because at apex. Hence you will get erratic behavior, and the problem is not in your program but in this domain DNS configuration that needs to be fixed.

$ dig MX designercakesbyapril.com +noall +ans
designercakesbyapril.com. 3h IN CNAME traff-1.hugedomains.com.
traff-1.hugedomains.com. 28s IN CNAME hdr-nlb9-41371129e8304c29.elb.us-east-1.amazonaws.com.

(and note how it fails as the last name does not have an MX record either)

You can see at https://dnsviz.net/d/designercakesbyapril.com/YjpWYQ/dnssec/ (an online DNS troubleshooting tool) how broken this domain name is (15 errors, 6 warnings).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement