Search code examples
javajakarta-mailsendmail

Using Java mail API


I am using Java mail API to send a mail using smtp.gmail.com as host. When I use the below code and enable TLS it works fine but if I disable it, it's throwing Must issue a STARTTLS command first error.

       mailProperties.put("mail.smtp.starttls.enable", "true");

What does it mean? So to connect to Gmail I should set this flag always? And also to connect to any mail server should we always enable secure connection (TLS or SSL)?


Solution

  • The GMail smtp server seems to require TLS authentication. (See here). Which means that the server will reject your query if you do not enable TLS.

    So yes, to connect to GMail you need to enable TLS. For a general mail server there usually is no need.