MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1

I know this is a very late answer, but want to share my experience when I face the same issue and tried almost Java core mail api, Apache mail api and Spring also for MimeMessage.

I tried 50+ times with Java JDK 1.7.0_80 and repeatedly failed with above subjected exception then I moved to JDK 1.8.0_151.

Now, before illustrating the source code, I will like to share my configuration.

Go to Gmail > Settings > Other Google Account settings under Accounts and Import > Sign-in & security.

Allow less secure apps: OFF (Means my application is not less secure)

2-Step Verification: Off

App Passwords (Click on it, and Google will generate a 16 characters long password for you, later done, change your gmail password with this 16 char long password (which is without any space)).

Google Generated App Password

Now, my source code is:

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;
import java.util.List;
import java.util.Properties;

public class Email {

    public final void prepareAndSendEmail(String htmlMessage, String toMailId) {

        final OneMethod oneMethod = new OneMethod();
        final List<char[]> resourceList = oneMethod.getValidatorResource();

        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(465);

        mailSender.setUsername(String.valueOf(resourceList.get(0)));
        mailSender.setPassword(String.valueOf(resourceList.get(1)));

        //from email id and password
        System.out.println("Username is : " + String.valueOf(resourceList.get(0)).split("@")[0]);
        System.out.println("Password is : " + String.valueOf(resourceList.get(1)));

        Properties mailProp = mailSender.getJavaMailProperties();
        mailProp.put("mail.transport.protocol", "smtp");
        mailProp.put("mail.smtp.auth", "true");
        mailProp.put("mail.smtp.starttls.enable", "true");
        mailProp.put("mail.smtp.starttls.required", "true");
        mailProp.put("mail.debug", "true");
        mailProp.put("mail.smtp.ssl.enable", "true");

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setTo(toMailId);
            helper.setSubject("Welcome to Subject Part");
            helper.setText(htmlMessage, true);

            //Checking Internet Connection and then sending the mail
            if(OneMethod.isNetConnAvailable())
                mailSender.send(mimeMessage);
            else
                JOptionPane.showMessageDialog(null, "No Internet Connection Found...");
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

}

and the Spring mail debug loggings are below said:

Username is : exampleusername
Password is : abcdefghijklmnop
DEBUG: JavaMail version 1.6.0
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
220 smtp.gmail.com ESMTP 12sm62330270pfr.147 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465

EHLO Administrator
250-smtp.gmail.com at your service, [157.48.195.205]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: STARTTLS requested but already using SSL
DEBUG SMTP: protocolConnect login, host=smtp.gmail.com, [email protected], password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<Administrator@Administrator>
250 2.1.0 OK 12sm62330270pfr.147 - gsmtp
RCPT TO:<[email protected]>
250 2.1.5 OK 12sm62330270pfr.147 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354  Go ahead 12sm62330270pfr.147 - gsmtp
Date: Mon, 19 Feb 2018 18:55:29 +0530 (IST)
To: [email protected]
Message-ID: <2023168072.2.1519046734494@Administrator>
Subject: Welcome to Subject Part
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_0_1884507527.1519046720984"

------=_Part_0_1884507527.1519046720984
Content-Type: multipart/related; 
    boundary="----=_Part_1_1634862487.1519046721031"

------=_Part_1_1634862487.1519046721031
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
Your html page source code is here
</html>

------=_Part_1_1634862487.1519046721031--

------=_Part_0_1884507527.1519046720984--
.
250 2.0.0 OK 1519046738 12sm62330270pfr.147 - gsmtp
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 2.0.0 closing connection 12sm62330270pfr.147 - gsmtp

Also please check your internet connection, antivirus settings and firewall access for being uninterrupted, hope this will help many from being wasting time.


Fix these common JavaMail mistakes.

Follow the connection debugging tips in the JavaMail FAQ.

Most likely there's a firewall or anti-virus product that's preventing you from connecting.

If Tomcat is running with a Java security manager, the JavaMail FAQ has information about configuring security permissions. If that doesn't help, the JavaMail FAQ also has information about debugging problems with security permissions.

Did I mention that you should read the JavaMail FAQ? :-)