user3547425
user3547425

Reputation: 149

connecting to secure server from Java application without importing certificate to keystore

I have a java application running on JBOSS EAP 7.3 (Java-ee) jdk1.8

The app needs to send an email (using java.mail) by connecting to a secure mail server which uses certificate and username/login to connect.

When I try to do the connection, I get an error

"javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

I have the .cert file, but we have several servers that this application is deployed on, and it is not possible (at least currently) to import the certificate into the keystore on each server. Is there a way to import it or embed it in the client app itself, without having to touch the servers?

This is the method that is doing the call:

    public static void sendEmail(Session session, String toEmail, String subject, String body){
            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("[email protected]", "NoReply-JD"));
              msg.setReplyTo(InternetAddress.parse("[email protected]", false));
              msg.setSubject(subject, "UTF-8");
              msg.setText(body, "UTF-8");
              msg.setSentDate(new Date());
              msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
              System.out.println("Message is ready");
              Transport.send(msg);  
    
              System.out.println("EMail Sent Successfully!!");
            }
            catch (Exception e) {
              e.printStackTrace();
            }
        }
}

Upvotes: 0

Views: 31

Answers (0)

Related Questions