title: JavaMail - java.net.ConnectException: Connection timed out: connect
body: <p>This is my code. And though it should be simple enough to send a mail, but I have been encountering the error mentioned below. And I have tried searching everywhere, but with no results.
  Thanks in advance !</p>
  
  <pre><code>import java.util.Properties;  
  import javax.mail.*;  
  import javax.mail.internet.*;  
  
  public class SendMailBySite {  
   public static void main(String[] args) {  
  
   String to="abc@gmail.com";//change accordingly  
  
    //Get the session object  
    Properties props = new Properties();  
    props.put("mail.smtp.host", "smtp.gmail.com");  
    props.put("mail.smtp.socketFactory.port", "465");  
    props.put("mail.smtp.socketFactory.class",  
              "javax.net.ssl.SSLSocketFactory");  
    props.put("mail.smtp.auth", "true");  
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", "465");  
  
    Session session = Session.getDefaultInstance(props,  
     new javax.mail.Authenticator() {  
     protected PasswordAuthentication getPasswordAuthentication() {  
     return new PasswordAuthentication("abc@gmail.com","********");//change accordingly  
     }  
    });  
  
    //compose message  
    try {  
     MimeMessage message = new MimeMessage(session);  
     message.setFrom(new InternetAddress("abc@gmail.com"));//change accordingly  
     message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
     message.setSubject("Hello");  
     message.setText("Testing.......");  
  
     //send message  
     Transport.send(message);  
  
     System.out.println("message sent successfully");  
  
    } catch (MessagingException e) {throw new RuntimeException(e);}  
  
   }  
  }
  </code></pre>
  
  <p><strong>This is the error encountered :</strong></p>
  
  <blockquote>
    <p>java.net.ConnectException: Connection timed out: connect</p>
    
    <p>at java.net.DualStackPlainSocketImpl.connect0(Native Method)</p>
    
    <p>at
    java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)</p>
    
    <p>at
    java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)</p>
    
    <p>at
    java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)</p>
    
    <p>at
    java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)</p>
    
    <p>at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)</p>
    
    <p>at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)</p>
    
    <p>at java.net.Socket.connect(Socket.java:579)</p>
    
    <p>at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)</p>
    
    <p>at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)</p>
    
    <p>at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)</p>
    
    <p>at ftpserver.FTPServer.main(FTPServer.java:25)</p>
  </blockquote>