Search code examples
javasmtpjakarta-mail

Could not connect to Gmail SMTP host


I am getting issue while sending email using Gmail email SMTP port and JakartaMail.

Code:

final String email = "xxxx@xxx.com";
final String pass = "xxxxx";
  
public Email() {
    this.callerObj = this;
    this.msg = null;
    this.mp = null;
    this.props = null;
    this.session = null;
    this.props = new Properties();
    this.props.setProperty("mail.smtp.auth", "true");  
    this.props.setProperty("mail.smtp.starttls.enable", "true"); 
    this.props.setProperty("mail.smtp.host", "smtp.gmail.com"); 
    this.props.setProperty("mail.smtp.port", "465");  
    this.props.setProperty("mail.debug", rbApp.getString("app.mail.debug"));
    this.session = Session.getInstance(this.props, null);
}

.....

session = Session.getInstance(props, new javax.mail.Authenticator() {  
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(email, pass);
    }
});
....

Error message:

MessagingException caught >> Fail to send Email >> Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

Here is my SMTP server configuration in Thunderbird:

enter image description here


Solution

  • Try port 587 instead of port 465. (A guess based on starttls and SMTP knowledge without jakarta knowledge).

    starttls suggests support for STARTTLS smtp command which means port 587 (or 25).
    Port 465 is for smtps - encryption is started before any SMTP level communication.