Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JavaMail with TLS

I found several other questions on SO regarding the JavaMail API and sending mail through an SMTP server, but none of them discussed using TLS security. I'm trying to use JavaMail to send status updates to myself through my work SMTP mail server, but it requires TLS, and I can't find any examples online of how to use JavaMail to access an SMTP server that requires TLS encryption. Can anyone help with this?

like image 839
dancavallaro Avatar asked Jan 04 '09 17:01

dancavallaro


People also ask

Is JavaMail deprecated?

The App Engine Mail API (which also supports JavaMail) has already been deprecated. Instead, GCP recommends to use a third-party mail provider such as: SendGrid.

How do I send an email through TLS?

Add the Secure transport (TLS) compliance setting to always use TLS for email sent to and from domains and addresses that you specify. When composing a new Gmail message, a padlock image next to the recipient address means that the message will be sent with TLS.

Which protocol is used to receive the messages in JavaMail?

IMAP: Acronym for Internet Message Access Protocol. It is an advanced protocol for receiving messages. It provides support for multiple mailbox for each user, in addition to, mailbox can be shared by multiple users. It is defined in RFC 2060.


1 Answers

We actually have some notification code in our product that uses TLS to send mail if it is available.

You will need to set the Java Mail properties. You only need the TLS one but you might need SSL if your SMTP server uses SSL.

Properties props = new Properties(); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.auth", "true");  // If you need to authenticate // Use the following if you need SSL props.put("mail.smtp.socketFactory.port", d_port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); 

You can then either pass this to a JavaMail Session or any other session instantiator like Session.getDefaultInstance(props).

like image 65
Sarat Avatar answered Oct 10 '22 11:10

Sarat