title: Sending Email via gmail smtp server in JAVA
body: <p>What is the problem with this code? Somehow it is getting in to an infinity loop at the line    <code>Transport.send(message);</code> line, no error message, no exception, just maybe infinite loop (i don't know because i don't wait more than 5-10minute)</p>
  
  <pre><code>final String username = "&lt;mail_name&gt;";
  final String password = "&lt;password&gt;";
  
  Properties props = new Properties();
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.port", "465");
  
  Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }
          });
  
  try {
  
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("&lt;mail_from&gt;@gmail.com"));
      message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse("&lt;mail_to&gt;@gmail.com"));
      message.setSubject("Test Subject");
      message.setText("Test");
  
      Transport.send(message);
  
      System.out.println("Done");
  
  } catch (MessagingException e) {
      throw new RuntimeException(e);
  }
  </code></pre>