What is mail.smtp.starttls.enable

mail.smtp.starttls.enable is a Java property that controls whether to use STARTTLS (Transport Layer Security) encryption when connecting to a mail server (SMTP) using JavaMail.

In JavaMail, mail.smtp.starttls.enable is a boolean property that defaults to false. When set to true, JavaMail will use STARTTLS to encrypt the connection to the mail server before sending the mail.

Here's what happens when mail.smtp.starttls.enable is set to true:

  1. JavaMail connects to the mail server using the default SMTP port (usually 25).
  2. The mail server responds with a greeting, which includes the supported encryption methods.
  3. JavaMail sends a STARTTLS command to the mail server, requesting that the connection be upgraded to a TLS (Transport Layer Security) connection.
  4. The mail server responds with a success message, and the connection is upgraded to a TLS connection.
  5. The mail server and JavaMail negotiate a shared secret key and exchange encrypted data.

By setting mail.smtp.starttls.enable to true, you ensure that the connection to the mail server is encrypted, which helps protect your email credentials and data from being intercepted or tampered with.

Here's an example of how to set this property in Java:

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);

Note that some mail servers may not support STARTTLS, so it's essential to check the mail server's documentation to ensure that it supports this feature.