We have a need to create SMTP client which will be authorized by public/private key.My code looks like below, but server is not seeing the cert being passed and rejecting the request. can someone help me with the correct implementation? I have imported both public and private and created keystore.p12 file using openssl command: openssl pkcs12 -export -out keystore.p12 -inkey private.key -in public.crt -name “test”
Properties props = new Properties();
props.put("mail.smtp.host", "test.test.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
try {
// Load the keystore
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(this.class.getClassLoader().getResourceAsStream("keystore.p12"), "password".toCharArray());
// Create a MailSSLSocketFactory and set the keystore
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "password".toCharArray());
socketFactory.setKeyManagers(keyManagerFactory.getKeyManagers());
//socketFactory.setKeyStorePassword("password");
// Set the socket factory
props.put("mail.smtp.ssl.socketFactory", socketFactory);
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]", false));
message.setSubject("Testing Subject");
message.setText("Test Mail");
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
1