12

Try send mail via 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);
}

It throw exception

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

Server available:

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 also work.

Similar .net code work as expected.

I have no ideas what going wrong....

5
  • Are you by any chance behind a proxy? Commented Sep 12, 2013 at 12:32
  • 1
    I'm trying to connect via telnet to your email server at ports 25 and 587 (most common ports) and I can't but ping works perfectly. Are you sure it is accepting connections right now? Commented Sep 12, 2013 at 12:56
  • No, proxy absent. Connect to this mail server should be possible only from our corporate network. Not sure why it ping-ed. Commented Sep 12, 2013 at 13:38
  • This problem doesn't seem Java related, you should first try connecting with a well known mail client (for instance Thunderbird) and first rule out it's a problem with the server accepting connections. Commented Sep 12, 2013 at 13:48
  • I have outlook connected to this server. Also I have some .net tools, which use this server for send mails(from my computer). All of them work correct. :( Also I have 'blat' program, which also work correct and send mails via this server. Commented Sep 12, 2013 at 15:26

1 Answer 1

37

The answer is: java prefer IPv6, but something wrong with it in our company. Adding property java.net.preferIPv4Stack=true solves problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome... this helped me derive a solution to the same error I was having with Jenkins all of a sudden not sending emails. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.