Do we have example of using sslcontext-kickstart as SSLSocketFactory for javax.mail #321
-
Hi javax.mail have pretty simple property based interface for SSL
There are no obvious way to pass SslSocketFactory created by SSLFactory.builder() other than creating your own wrapper class for each interfaces in the system. Anybody had a positive experience with this library and javax.mail? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hi Vlad! I just tried out locally and it works with SSLFactory without the need of creating wrapper classes. I am pasting here my full example, hopefully it will also work on your side. I am just passing the SSLContext from the SSLFactory to the default method so at some other point in time Javax Mail can create a sslsocketfactory from the defaul sslcontext. import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLContext;
import java.util.Properties;
public class App {
public static void main(String[] args) throws MessagingException {
SSLFactory sslFactory = SSLFactory.builder()
.withDefaultTrustMaterial()
.withSystemTrustMaterial()
.build();
SSLContext.setDefault(sslFactory.getSslContext());
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[my-email-address]@gmail.com", "[my-password]");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[my-email-address]@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[some-other-email-address]@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Test Mail");
Transport.send(message);
System.out.println("Done");
}
} I added two trustmaterial options to the SSLFactory so I could add a debug statement in
Can you try on your side and share your results? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply Hakan. In fact in javax.mail version 1.6.7 we can pass factory instances instead of class literal.
For the new specification 'jakarta.mail'.. angus-mail approach is the same. For the records Documentation here https://eclipse-ee4j.github.io/angus-mail/docs/api/org.eclipse.angus.mail/org/eclipse/angus/mail/smtp/package-summary.html I just verified the same with SSL on sendgrid without using jvm certificates .
* Negative cases with non matching certificates verified as well. |
Beta Was this translation helpful? Give feedback.
-
Good Point.
The code above also have a mistake .
This version looks cleaner.
|
Beta Was this translation helpful? Give feedback.
-
Now that I have configuration setup as in code above we can easily control protocol used per connection. e.g.
I tested with most populate public services:
And the results are the same . Works as of 2023.04: Can't be negotiated: Google support TLS on pors 587... I will dig there next. Question is there an easy way to get get callback/log on actual protocol name that had been negotiated in SSL connections? |
Beta Was this translation helpful? Give feedback.
Good Point.
Yes it does work as expected.
The code above also have a mistake .
If you intend to use SSL ports and omit 'mail.smtp.socketFactory' it will hangs ... And setting connection timeouts will not help.
So for clarity proper code should set this
This version looks cleaner.