The four SMTP properties and an authenticated Session
The four SMTP properties and an authenticated Session
Answer
Properties props = new Properties(); props.put("mail.smtp.host", server); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } });
getInstance opens no connection; it only records configuration. The anonymous Authenticator is a callback the transport invokes later, once it has seen the server advertise AUTH — which is why a wrong password fails at send time and not here.
Jakarta Mail API §Session, §MimeMessage, §Transport