I need to send an email using Javamail and TLS (not STARTTLS, but a dedicated smtp port just for SSL/TLS!). I only managed to find examples for gmail, that however use STARTTLS. Can somebody please post an example for normal SSL/TLS? Thank you very much!
Share
The official examples for JavaMail with Gmail use SMTPS (i.e. SMTP over SSL/TLS on a dedicated port) and not STARTTLS. Essentially, the properties using JavaMail should be
mail.smtps.*instead ofmail.smtp.*.If you want to force a specific version of SSL/TLS, for example TLSv1.0, you’ll need to create your own
SSLSocketFactory, possibly wrapping the defaultSSLSocketFactory(or anything else you would have customised), but you need to callsslSocket.setEnabledProtocols(new String[] { "TLSv1" })before returning the socket.You’ll need to pass that
SSLSocketFactoryeither as an instance via themail.smtps.ssl.socketFactoryconfiguration property, or as a fully qualified class name viamail.smtps.ssl.socketFactory.class(in this case, you class must implement a static method calledgetDefault).To prevent MITM attacks, you also need to make the client verify the server host name: you need to set
mail.smtps.ssl.checkserveridentitytotrue, since it seems to befalseby default.