While sending mail, I am getting this error:

java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

Here is my code:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("email", "password");
        }
    });

try {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("email"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(this.to));
    message.setSubject("Testing");
    message.setText("Hey, this is the testing email.");

    Transport.send(message);
    // ...
}

What is the cause of this error, and how can the problem be resolved?

Edit
Report