ローカルにフリーのメールサーバを立て、アプリケーションサーバ上のJavaクラスでメール送信処理を行ったところ、以下のエラーとなりました。
java.net.SocketException: Network is unreachable: connect
ネットワークに繋がってなさそうな感じのエラーです。
スタンドアロンな環境にアプリケーションサーバもメールサーバも立てていたので、ゲートウェイとかはあまり関係ないと思っていたのですが、何故かダメでした。
一応、適当なメーラでの送受信は可能だったのでSMTPのアドレスやポート、認証回りは問題なさそうに思えました。
調べてみると、以下のページがヒットしました。
email - Sending mail: java.net.SocketException: Network is unreachable: connect - Stack Overflow
The answer is: java prefer IPv6, but something wrong with it in our company. Adding property java.net.preferIPv4Stack=true
solves problem.
Java Mail mystery - SMTP blocked? - Stack Overflow
This is the root of the problem - Java prefers IPv6, which it cannot use to connect to the internet.
To fix this, do either one (or both) of these things:
Doing either one of these will fix the problem.
ビンゴでした。アプリケーションサーバのJVMオプションに-Djava.net.preferIPv4Stack=true
を指定することで、無事メール送信処理が成功しました。
以前も似たような環境で確認していましたが、そのときは問題なく成功していたために、今回は苦戦しました。