JavaMail fails from IPv6 only host to dual stack environment

600 views Asked by At

I've written a small test program to try to send a mail message using JavaMail. I'm running into odd behavior in Java and I'm not sure how to fix it. The basic setup is that my test program is running from an IPv6 only linux box but it is trying to send to an IPv4 & IPv6 SMTP server in a dual-stack environment. What I mean by dual stack environment is that the DNS server has an A and a AAAA record with the same hostname for the smtp server, as far as I know this should be legal. When I run my program with both A and AAAA records I get the following exception:

javax.mail.MessagingException: Could not connect to SMTP host: <MY Fully qualified domain name>, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
Caused by: java.net.SocketException: Network is unreachable
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 8 more

However, If I remove the A record then I am able to reach the SMTP server without a problem. No matter what values I give the java properties 'java.net.preferIPv6Addresses' 'java.net.preferIPv4Stack' the program will only be able to access the smtp server when there is only an AAAA record. How can I tell Java to use the AAAA record and prevent it from getting a socket exception when both A and AAAA records exist for the same hostname?

0

There are 0 answers