title: java mail gmail
body: <p>I want to send a mail in my java program trough google's smtp server, but it seems stuck at sending the mail. Can someone tell me why pls?</p>
  
  <p>this is the function to send the mail:</p>
  
  <pre><code>     public void sendMail(){
              String from = "xxx@gmail.com";
      String to = "xxx@hotmail.com";
      String subject = "Test";
      String message = "A test message";
  
      SendMail sendMail = new SendMail(from, to, subject, message);
      sendMail.send();
  }
  </code></pre>
  
  <p>And this is the class</p>
  
  <pre><code>public class SendMail {
  private String from;
  private String to;
  private String subject;
  private String text;
  
  public SendMail(String from, String to, String subject, String text){
      this.from = from;
      this.to = to;
      this.subject = subject;
      this.text = text;
  }
  
  public void send(){
  
      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.auth", "true");
      props.setProperty( "mail.smtp.port", "587" );
      props.put("mail.smtp.starttls.enable", "true");
      Session session = Session.getDefaultInstance(props);
  
      new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(from, "MyPasswordGoesHere");
      }
        };
  
    try {
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject(subject);
      message.setText(text);
  
      Transport.send(message);
      System.out.println("message sent successfully");
      } catch (MessagingException e) {
      throw new RuntimeException(e);
       }
        }
          }
  </code></pre>
  
  <p>Thanks in advance!</p>