title: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 (JSP)
body: <p>I'm just trying to send a confirmation email, with JAVA BEANS (JSP), but I get this annoying error:</p>
  
  <blockquote>
    <p>could not connect to host "smtp.gmail.com", port: 465, response: -1</p>
  </blockquote>
  
  <p>Got this class</p>
  
  <pre><code>import java.util.Properties;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.AddressException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage; 
  
  public class MailServiceBean {
    private String fromEmail, password;
    private String toEmail, subject, message;
  
  public MailServiceBean(){
      fromEmail = "dumb@gmail.com";
      password = "dumbpass";
      toEmail = "";
      subject = "";
      message = "";
  }
  
  public void sendConfirmationMail(){
      try {
          Properties properties = System.getProperties();
          properties.put("mail.smtp.host", "smtp.gmail.com");
          properties.put("mail.smtp.socketFactory.port", "465");
          properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");    
          properties.put("mail.smtp.auth", "true");
          properties.put("mail.smtp.port", "465");                        
          Session mailSession = Session.getInstance(properties,
                  new javax.mail.Authenticator() {
                      @Override
                      protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(fromEmail,password);
          }
          });
          mailSession.setDebug(true);
          Message mail = new MimeMessage(mailSession);      
          mail.setFrom(new InternetAddress(fromEmail));
          mail.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
          mail.setContent(message, "text/html");
          mail.setSubject(subject);
          Transport.send(mail);
          System.out.println("Ya se mando");
      } catch (MessagingException ex) {
          Logger.getLogger(MailServiceBean.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  
  /**
   * @return the fromEmail
   */
  public String getFromEmail() {
      return fromEmail;
  }
  
  /**
   * @param fromEmail the fromEmail to set
   */
  public void setFromEmail(String fromEmail) {
      this.fromEmail = fromEmail;
  }
  
  /**
   * @return the password
   */
  public String getPassword() {
      return password;
  }
  
  /**
   * @param password the password to set
   */
  public void setPassword(String password) {
      this.password = password;
  }
  
  /**
   * @return the toEmail
   */
  public String getToEmail() {
      return toEmail;
  }
  
  /**
   * @param toEmail the toEmail to set
   */
  public void setToEmail(String toEmail) {
      this.toEmail = toEmail;
  }
  
  /**
   * @return the subject
   */
  public String getSubject() {
      return subject;
  }
  
  /**
   * @param subject the subject to set
   */
  public void setSubject(String subject) {
      this.subject = subject;
  }
  
  /**
   * @return the message
   */
  public String getMessage() {
      return message;
  }
  
  /**
   * @param message the message to set
   */
  public void setMessage(String message) {
      this.message = message;
  }
  }
  </code></pre>
  
  <p>I call MailServiceBean class in this page:</p>
  
  <pre><code>&lt;%@page import="prueba.MailServiceBean"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%
     MailServiceBean mail = new MailServiceBean();
     mail.setToEmail("toMail@hotmail.com");
     mail.setSubject("Dumb Subject");
     mail.setMessage("Testing mail");
     mail.sendConfirmationMail();
     response.sendRedirect("pagina.html");
  %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;JSP Page&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Hello World!&lt;/h1&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How can I correct this? I must confess that a couple of times the code was working, now it's useless. :(</p>