4 Replies Latest reply: Mar 27, 2013 3:59 PM by 995346 RSS

    Could not connect to SMTP host, port java.net.ConnectException

    995346
      Java Mail always worked for me but now it's throwing this annoying Exception:

      *//Exception:*

      run:
      DEBUG: setDebug: JavaMail version 1.4.6
      DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
      DEBUG SMTP: useEhlo true, useAuth true
      DEBUG SMTP: trying to connect to host "xxxxxx", port 25, isSSL false
      Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: xxxxxx, port: 25;
      nested exception is:
      java.net.ConnectException: Operation timed out
      at vesso.mail.TestMail.sendMail(TestMail.java:79)
      at vesso.mail.TestMail.main(TestMail.java:26)
      Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mail.mentorlicitacao.com.br, port: 25;
      nested exception is:
      java.net.ConnectException: Operation timed out
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1962)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
      at javax.mail.Service.connect(Service.java:295)
      at vesso.mail.TestMail.sendMail(TestMail.java:71)
      ... 1 more
      Caused by: java.net.ConnectException: Operation timed out
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
      at java.net.Socket.connect(Socket.java:579)
      at java.net.Socket.connect(Socket.java:528)
      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:1928)
      ... 4 more
      Java Result: 1
      BUILD SUCCESSFUL (total time: 1 minute 19 seconds)




      *//Code:*

      final String host = "xxxx";
      final int port = 25;
      final String username = "xxxx";
      final String password = "xxxx";

      Properties props = new Properties();

      props.put("mail.smtp.auth", "true");

      Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password);
      }
      });
      session.setDebug(true);

      try {

      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress(username));
      message.setRecipients(Message.RecipientType.TO,
      InternetAddress.parse(username));
      message.setSubject("Teste");
      message.setText("Bla, Bla, Bla!");

      Transport transport = session.getTransport("smtp");
      transport.connect (host, port, username, password);
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();


      System.out.println("Done");

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

      This does not seem to be a code problem.
      I've tried to connect with Mail.app (using MacOS), and it worked fine.
      The code also worked on a Linux machine. What is happening? Maybe a JVM issue, I'm running 1.7.0._17.

      What can I do?