1

While writing an Email application in java I getting this exception:

javax.mail.MessagingException: Could not connect to SMTP host: mail.simsystech.com, port: 8443;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:317)

& here is my code(follow this tutorial link) :

          Properties properties = System.getProperties();
          properties.put("mail.smtp.starttls.enable", "true");
          properties.put("mail.transport.protocol", "smtp");
          properties.setProperty("mail.smtp.host", host);
//        properties.put("mail.smtp.port", "8443");
          properties.setProperty("mail.smtp.auth", "true");
          properties.setProperty("mail.smtp.starttls.enable", "true");

          final String user = "abc@xyz.com";
          final String password = "*****";

          Authenticator authenticator = new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user,password);
                }
            };


          Session session = Session.getDefaultInstance(properties,authenticator);


          try{
              MimeMessage message = new MimeMessage(session);

              message.setFrom(new InternetAddress(sid));

              message.addRecipient(Message.RecipientType.TO,
                                       new InternetAddress(rid));

              message.setSubject(subject);

              BodyPart messageBodyPart = new MimeBodyPart();

              messageBodyPart.setText(text);

              Multipart multipart = new MimeMultipart();

              multipart.addBodyPart(messageBodyPart);


              messageBodyPart = new MimeBodyPart();
//            String filename = "file.txt";
              DataSource source = new FileDataSource(file);
              messageBodyPart.setDataHandler(new DataHandler(source));
              messageBodyPart.setFileName(file);
              multipart.addBodyPart(messageBodyPart);

              // Send the complete message parts
              message.setContent(multipart );


              // Now set the actual message
//            message.setText(text);

              // Send message
              Transport.send(message);
              System.out.println("Sent message successfully....");
           }catch (MessagingException ex) {
              ex.printStackTrace();
           }

Though I have gone through these links of stackoverflow : 1, 2

Any idea why I'm getting this error... :( :(

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1645434
  • 193
  • 3
  • 13
  • 4
    Did you try to e.g. `telnet` the host (from the system executing your code)? The error is fairly generic so I vote for a network issue... – home Oct 04 '12 at 13:24
  • Are you talking about "mail.simsystech.com" -this ? – user1645434 Oct 04 '12 at 13:26
  • Yes, I do. This is what I'm talking about... – home Oct 04 '12 at 13:29
  • could I use some other smtp server+it's port for my application ?1st I have made a simple application,just with the help of recient-id and sender-id I was able to send text message,but whenever I add attachment option...from that time I'm getting this kind of weird exception....Ant inputs ? – user1645434 Oct 04 '12 at 13:36
  • If you just want to perform a local test, install a mail server on your PC... regarding the error - it does only occur if you add an attachment? – home Oct 04 '12 at 13:45
  • so...... any remedy on this context ? – user1645434 Oct 04 '12 at 13:52

2 Answers2

3

Any idea why I'm getting this error...

The two most obvious potential causes are:

  • You have the wrong hostname and/or port number for the SMTP host. If there isn't an SMTP service running on that host using that port, then you will have to use something else.

  • Your computer cannot connect to that host on that port because a firewall is blocking your access.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • One more thing, **use something else** -could I use some other smtp server+it's port for my application ?1st I have made a simple application,just with the help of recient-id and sender-id I was able to send text message,but whenever I add attachment option...from that time I'm getting this kind of weird exception....Ant inputs ? – user1645434 Oct 04 '12 at 13:35
  • @user1645434 - *"How to unblock it ?"* - it depends where the firewall is, how it is implemented and so on. – Stephen C Oct 04 '12 at 13:55
  • @user1645434 - *"could I use some other smtp server+it's port for my application?"*. That is one option. But if the problem is firewalls then switching servers might not help. – Stephen C Oct 04 '12 at 13:57
  • could you provide other link for smtp server+it's port. – user1645434 Oct 04 '12 at 14:04
1

Assuming that no network issues occur with your connection (you can use telnet to make sure of it) then...

The problem must be because of the properties.setProperty("mail.smtp.starttls.enable", "true");

According to javadoc of com.sun.mail.smtp ,

If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. Note that an appropriate trust store must configured so that the client will trust the server's certificate. Defaults to false.

And below says for mail.smtp.starttls.required

If true, requires the use of the STARTTLS command. If the server doesn't support the STARTTLS command, or the command fails, the connect method will fail. Defaults to false.

Try removing the above property if your provider doesn't support it.

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125