How do I properly setup javamail to use STARTTLS?

13,456

The issue I was having with STARTTLS was caused by me getting a secure transport:

transport = session.getTransport("smtps");

After changing it to "smtp" i was able to use STARTTLS.

Share:
13,456
Knoxie
Author by

Knoxie

Updated on June 04, 2022

Comments

  • Knoxie
    Knoxie almost 2 years

    When setting up my session I am setting the starttls.enable and .required properties but when the connection happens it should fail according to the documentation:

    mail.smtp.starttls.enable boolean If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. Note that an appropriate trust store must configured so that the client will trust the server's certificate. Defaults to false." http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html

    props = new Properties();
    props.put("mail.smtps.host", MAILSERV);
    props.put("mail.smtps.socketFactory.port", 465);
    props.put("mail.smtps.auth", "true");
    props.put("mail.smtps.port", 465);
    props.put("mail.smtps.socketFactory.fallback", "false");
    props.put("mail.smtps.socketFactory.class","utils.DummySSLSocketFactory");
    props.put("mail.smtps.quitwait", "false");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");
    Session session = Session.getInstance(props, authenticator);  
    
    
    transport = session.getTransport("smtps");
    transport.connect(mailServer, port, username, password);
    transport.sendMessage(message, message.getAllRecipients());
    

    So what am I doing wrong that is allowing me to use Starttls when its not supported by the server?