JavaMail library defaults to port 25 instead of custom port when connecting to Gmail SMTP

I’m working on a Groovy script that sends emails through Gmail’s SMTP server using JavaMail. The issue is that my application keeps trying to connect on port 25 even though I’ve configured it to use port 587.

Here’s the error message I’m getting:

DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
Caught: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25 (javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?))

I’ve verified that port 587 works fine with other email clients like Thunderbird using STARTTLS. Here’s my current implementation:

import javax.mail.*
import javax.mail.internet.*

private class EmailAuthenticator extends Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication('[email protected]', 'mypassword')
    }
}

def userEmail = "[email protected]"
def userPassword = "mypassword"
def smtpHost = "smtp.gmail.com"
def smtpPort = "587"
def recipientEmail = "[email protected]"
def emailSubject = "Test Message"
def emailBody = "Hello, this is a test email."

def properties = new Properties()
properties.put("mail.smtp.user", userEmail)
properties.put("mail.smtp.host", smtpHost)
properties.put("mail.smtp.port", smtpPort)
properties.put("mail.smtp.starttls.enable", "true")
properties.put("mail.smtp.debug", "true")
properties.put("mail.smtp.auth", "true")
properties.put("mail.smtp.socketFactory.port", smtpPort)
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
properties.put("mail.smtp.socketFactory.fallback", "false")

def authenticator = new EmailAuthenticator()
def mailSession = Session.getInstance(properties, authenticator)
mailSession.setDebug(true)

def message = new MimeMessage(mailSession)
message.setText(emailBody)
message.setSubject(emailSubject)
message.setFrom(new InternetAddress(userEmail))
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail))
Transport.send(message)

What could be causing JavaMail to ignore my port configuration and default to port 25? Any suggestions would be really helpful!

The debug output shows useAuth false - your authentication isn’t being recognized. JavaMail is falling back to default settings because of configuration conflicts. Those socketFactory properties are for direct SSL connections on port 465, not STARTTLS on port 587. When JavaMail sees this mismatch, it ignores your port setting and defaults back to port 25. Remove those three socketFactory lines and your STARTTLS config should work fine. I’ve seen this happen tons of times - socketFactory settings override your port when the settings don’t match.

Your socket factory config is the problem. Setting mail.smtp.socketFactory.class to SSLSocketFactory forces JavaMail to use SSL right away, which crashes with STARTTLS on port 587. Just remove all three socketFactory properties - they’re for port 465 (SSL), not 587 (STARTTLS). STARTTLS handles encryption after the plain connection starts. Had this exact issue last year and ditching those socketFactory lines fixed it instantly.

yep, sounds like you’re mixing things up. try getting rid of the socketFactory settings - port 587 needs starttls instead of SSL. those SSL settings are likely screwing things up.