web-dev-qa-db-ja.com

メールの送信:Java.net.SocketException:ネットワークに到達できません:接続

Javax.mail経由でメールを送信してみてください。

Properties props = new Properties();
props.put("mail.smtp.Host", "xxxxx");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "false");

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("xx", "xx");
    }
  });

try {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("xxxxx"));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xxxxx"));
    message.setSubject("Subject");
    message.setText("Body");

    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}

例外をスローします

Exception in thread "main" Java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP Host: srv-mail.imb.invention.com, port: 25;
  nested exception is:
        Java.net.SocketException: Network is unreachable: connect
        at foo.SendMailTest.main(SendMailTest.Java:41)
Caused by: javax.mail.MessagingException: Could not connect to SMTP Host: xxx.xxxx.zzzzz.com, port: 25;
  nested exception is:
        Java.net.SocketException: Network is unreachable: connect
        at com.Sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.Java:1961)
        at com.Sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.Java:654)
        at javax.mail.Service.connect(Service.Java:295)
        at javax.mail.Service.connect(Service.Java:176)
        at javax.mail.Service.connect(Service.Java:125)
        at javax.mail.Transport.send0(Transport.Java:194)
        at javax.mail.Transport.send(Transport.Java:124)
        at foo.SendMailTest.main(SendMailTest.Java:36)
Caused by: Java.net.SocketException: Network is unreachable: connect
        at Java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at Java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at Java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at Java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at Java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at Java.net.PlainSocketImpl.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:321)
        at com.Sun.mail.util.SocketFetcher.getSocket(SocketFetcher.Java:237)
        at com.Sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.Java:1927)
        ... 7 more

利用可能なサーバー:

C:\work\test>nc.exe xxx 25
220 xxx.zzz.aaaaaaa.com Microsoft ESMTP MAIL Service ready at Thu, 12 Sep 2013 15:10:45 +0300

pingも機能します。

同様の.netコードは期待どおりに機能します。

何がうまくいかないのか分かりません。

11

答えは次のとおりです。Java IPv6を好むが、当社ではIPv6に問題があります。プロパティの追加Java.net.preferIPv4Stack=true問題を解決します。

38