SSL Handshake Exception When Sending Email Through Gmail SMTP with JavaMail

I’m having trouble with SSL certificate validation while trying to send emails using JavaMail API with Gmail’s SMTP server. Here’s my implementation:

final String userEmail = "[email protected]";
final String userPassword = "mypassword123";

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

Session mailSession = Session.getInstance(config,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userEmail, userPassword);
        }
    }
);

try {
    MimeMessage email = new MimeMessage(mailSession);
    email.setFrom(new InternetAddress("[email protected]"));
    email.setRecipients(Message.RecipientType.TO,
                       InternetAddress.parse("[email protected]"));
    email.setSubject("Test Email Subject");
    email.setText("Hello there,\n\nThis is a test email message.");
    Transport.send(email);
    System.out.println("Email sent successfully");
} catch (MessagingException ex) {
    throw new RuntimeException(ex);
}

I keep getting this error message:

Could not convert socket to TLS

The error seems to be related to SSL certificate validation issues. The stack trace shows SSLHandshakeException and mentions problems with certificate path validation. Has anyone encountered this issue before? What could be causing this SSL handshake failure when connecting to Gmail’s SMTP server?

Hit the same SSL nightmare across different server setups. Usually happens when Java’s truststore doesn’t know Gmail’s certificate chain or intermediate certs. I threw in mail.smtp.ssl.trust=* to skip cert validation completely - sketchy for production but shows if certs are the problem. Real fix is updating your JRE’s cacerts or setting up a custom truststore with Gmail’s certificates. Check if you’re behind corporate infrastructure doing SSL inspection too - that’ll wreck certificate chain validation. Sometimes just switching to port 465 with SSL instead of 587 with STARTTLS does the trick - different handshake that might play nicer with your setup.

check if ur using app passwords instead of ur regular password. google killed basic auth for gmail smtp last year, so regular passwords dont work anymore. also try adding mail.smtp.ssl.trust=smtp.gmail.com to ur properties - sometimes fixes cert validation issues.

Had this exact SSL nightmare a few months back when our notification system randomly died. Gmail SMTP SSL issues are usually environment stuff - Java version differences, certificate problems, or corporate firewalls breaking the handshake.

Skip the Java SSL debugging hell. I moved our entire email setup to Latenode. It handles Gmail authentication automatically, including OAuth2 that Google’s forcing on everyone.

Best part? No more hardcoded passwords or SSL certificate headaches. Latenode connects directly to Gmail’s API and manages all the security stuff. Takes 5 minutes vs hours of JavaMail troubleshooting.

Trigger emails from your Java app with simple HTTP calls to Latenode workflows. Way cleaner than managing SMTP connections and much more reliable.

I’ve battled Gmail SMTP SSL issues for years. The real problem isn’t certificates or Java versions - it’s that SMTP email delivery is fragile and Gmail constantly changes their security requirements.

Instead of patching JavaMail configs that break every few months, I automate email through Latenode. It connects directly to Gmail’s API and handles OAuth2 authentication automatically.

No SSL handshake failures, no app passwords, and better delivery rates. Your Java app just sends HTTP requests to trigger Latenode workflows that handle the email sending.

You also get proper error handling and retry logic built in. Way more reliable than maintaining SMTP connections that randomly fail in production.

This SSL handshake issue usually comes from outdated Java or restrictive JVM security settings. I hit the same problem deploying to production servers with older Java 8 builds. Gmail’s certificates use newer encryption that older Java versions can’t handle properly. Add these properties to fix the SSL config: mail.smtp.ssl.protocols=TLSv1.2 and mail.smtp.ssl.ciphersuites with modern cipher suites. Check your Java version supports TLS 1.2 - anything before Java 8u161 had known Gmail SSL issues. If you’re behind a corporate proxy or firewall, it might intercept SSL traffic and mess up the certificate chain. Network admins sometimes install custom certificates that break normal validation.