title: MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
body: <p>I need to send an email from my application using Gmail as the SMTP server.
  This is my mail connector class and I have set values in a separate property file</p>
  <pre><code>    public class EmailConnector {
      
  
      public static Session sessionCreate() {
          final String fromEmail = ConfigurationManager.getInstance().getProperty(EmailConfig.SENDER_EMAIL.toString());
  
          final String password = ConfigurationManager.getInstance().getProperty(EmailConfig.SENDER_PASSWORD.toString());
  
          Properties props = new Properties();
          props.put(&quot;mail.smtp.host&quot;, ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_HOST.toString()));
  
          props.put(&quot;mail.smtp.socketFactory.port&quot;,
                  ConfigurationManager.getInstance().getProperty(EmailConfig.SSL_PORT.toString()));
  
          props.put(&quot;mail.smtp.socketFactory.class&quot;,
                  ConfigurationManager.getInstance().getProperty(EmailConfig.SSL_FACTORY_CLASS.toString()));
  
          props.put(&quot;mail.smtp.auth&quot;,
                  ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_AUTHENTICATION.toString()));
  
          props.put(&quot;mail.smtp.port&quot;, ConfigurationManager.getInstance().getProperty(EmailConfig.SMTP_PORT.toString()));
  
          Authenticator auth = new Authenticator() {
              @Override
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(fromEmail, password);
              }
          };
          return Session.getDefaultInstance(props, auth);
      }
  }
  </code></pre>
  <p>Properties:</p>
  <pre><code>#Email send configuration
  SENDER_EMAIL = abcalerts@gmail.com
  SENDER_PASSWORD = abcalert321
  SMTP_HOST = smtp.gmail.com
  SSL_PORT = 465
  SMTP_AUTHENTICATION = true
  SMTP_PORT = 465
  SSL_FACTORY_CLASS = javax.net.ssl.SSLSocketFactory
  </code></pre>
  <p>Then I implemented a mail sender class, called &quot;GroupEmail.class&quot;</p>
  <pre><code>public class GroupEmail {
  
      public void sendEmail() throws IOException {
          String recipient = &quot;nirmalauwucst@gmail.com&quot;;
  
          Session session = EmailConnector.sessionCreate();
          /* subject of email */
          String emailSubject = &quot;ABC_Alert&quot;;
          try {
              MimeMessage msg = new MimeMessage(session);
              msg.addHeader(&quot;Content-type&quot;, &quot;text/HTML; charset=UTF-8&quot;);
              msg.addHeader(&quot;format&quot;, &quot;flowed&quot;);
              msg.addHeader(&quot;Content-Transfer-Encoding&quot;, &quot;8bit&quot;);
  
              msg.setFrom(new InternetAddress(&quot;abcalerts@gmail.com&quot;, &quot;ABC Alerts&quot;));
  
              msg.setReplyTo(InternetAddress.parse(&quot;abcalerts@gmail.com&quot;));
  
              msg.setSubject(emailSubject, &quot;UTF-8&quot;);
  
              msg.setSentDate(new Date());
              /* buyer email address */
              msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
  
              /* Create the message body part */
              msg.setText(&quot;A new Test-Alert from AB_Alerts&quot;);
  
              /* Send message */
              Transport.send(msg, &quot;abcalerts@gmail.com&quot;, &quot;abcalert321&quot;);
  
          } catch (MessagingException | UnsupportedEncodingException e) {
              SystemLogger.logErrorMessege(this, e);
          }
      }
  
  }
  </code></pre>
  <p>After all I called the &quot;GroupEmail.class&quot; in a place that I needed to trigger the email to be sent.</p>
  <pre><code>GroupEmail groupEmail = new GroupEmail();
          groupEmail.sendEmail(); 
  </code></pre>
  <p>I used Tomcat v8 server on localhost and when the application works, I got the below exception.</p>
  <pre><code>98656 [http-nio-8080-exec-9] ERROR it.nmad.test.abcalert.notification.template.email.GroupEmail  - Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1
  com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
    nested exception is:
      java.net.SocketException: Permission denied: connect
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
      at javax.mail.Service.connect(Service.java:388)
      at javax.mail.Service.connect(Service.java:246)
      at javax.mail.Service.connect(Service.java:195)
      at javax.mail.Transport.send0(Transport.java:254)
      at javax.mail.Transport.send(Transport.java:124)
  ..more
  </code></pre>