How to send mail using a certificate to authenticate on the SMTP server?

I need to send emails from backend. All the data I have: certificate, username, password, ports, server address, etc. can't understand/find how to tie a certificate to authenticate on the SMTPserver. For example, I receive a certificate from the store:
final Certificate certificate;
try {
 certificate = keyStoreService.loadCertificate(settings.getCertificateAlias());
} catch (KeyStoreServiceException e) {
 final String errMsg = "An error occurred during work with the Java KeyStore:" + e.toString();
 logger.error(errMsg, e);
 throw new RuntimeException(errMsg, e);
}


But have not found where it is bolted to the javax.mail.Session or even to some object that is used to send emails.

For example, my code for the opening of the session:
public Session openSession() {
 logger.trace("SSL Start");

 Properties props = new Properties();
 AppSettings settings = dbService.getAppSettings();
 props.put("mail.smtp.host", settings.getMailServer()); //SMTP Host

 final Certificate certificate;
 try {
 certificate = keyStoreService.loadCertificate(settings.getCertificateAlias());
 } catch (KeyStoreServiceException e) {
 final String errMsg = "An error occurred during work with the Java KeyStore:" + e.toString();
 logger.error(errMsg, e);
 throw new RuntimeException(errMsg, e);
}

 props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
 props.put("mail.smtp.port", settings.getMailServerPort()); //SMTP Port

 props.put("mail.debug" settings.getMailDebug());


 //create Authenticator object to pass in Session.getInstance argument
 Authenticator auth = new Authenticator() {
 //override the getPasswordAuthentication method
@Override
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(settings.getMailLogin(), settings.getMailPassword());
}
};
 return Session.getInstance(props, auth);
}

And so is sending the letter:
public static void sendEmail(Session Session, String toEmail, String subject, String body, String fromAlias, String fromEmail) {
 try {
 MimeMessage msg = new MimeMessage(session);
 //set message headers
 msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
 msg.addHeader("format", "flowed");
 msg.addHeader("Content-Transfer-Encoding", "8bit");
 msg.setFrom(new InternetAddress(fromEmail, fromAlias));
 msg.setReplyTo(InternetAddress.parse(fromEmail, false));
 msg.setSubject(subject, "UTF-8");
 if (body == null)
 body = "";
 msg.setText(body, "UTF-8");
 msg.setSentDate(new Date());
 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));

 logger.info("Message is ready");
Transport.send(msg);
 logger.info("EMail has been sent successfully!!");
 } catch (Exception e) {
 final String errMsg = String.format("An error occurred while sending an email with subject = '%s' and body = '%s', recipient = '%s': %s",
 subject, body, toEmail, e.toString());
 logger.error(errMsg, e);
}
}


Our system administrator tried to send mail out through postfix, and everything worked, He used the following postfix settings:
relayhost = XX.XX.XX.XX:25
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtp_generic_maps = hash:/etc/postfix/generic
smtp_tls_cert_file = /etc/postfix/cert.pem
local_header_rewrite_clients = static:all
April 7th 20 at 10:56
1 answer
April 7th 20 at 10:58
I'm sorry, but an error with the Java KeyStore you copied from the logs?
in fact, for such questions have a comment - Howell.Powlowski commented on April 7th 20 at 11:01

Find more questions by tags JavaE-mailSMTPDigital certificates