简体   繁体   中英

javax.mail.MessagingException: PKIX path building failed: SunCertPathBuilderException: unable to find valid certification path to requested target;

I get this Exception when I try to connect to the store.

As I look through forums, the solution to this error is to add the certificate to the truststore file of the used JVM.

The problem is I am new to this and I am not sure that I understand how I should do this exactly: Where should I get the certificate and how should I add it?

NB: I notice that when I run this using main method, it works fine, But when I set a Scheduler to call the method automatically. that's when I get the exception.

Thank you a lot for the help.

public void receiveMails() throws Exception
    {

        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.imaps.socketFactory.fallback", "false");
        props.setProperty("mail.imaps.ssl.enable", "false");
        props.setProperty("mail.imaps.socketFactory.port", "993");
        props.setProperty("mail.imaps.starttls.enable", "true");
        props.setProperty("mail.imaps.ssl.trust", "mailHost");

        try
        {
            
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
            store.connect("mailHost", "user@domain.com", "password");
//...

The Exception is:

javax.mail.MessagingException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target;
  nested exception is:
    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
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:479)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)
    

Try disabling your antivirus while doing this operation. This might be the cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question PKIX path building failed: SunCertPathBuilderException: unable to find valid certification path to requested target SSLHandshakeException: PKIX path building failed SunCertPathBuilderException: unable to find valid certification path to requested target PKIX building failed:sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target CXF:PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target? PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target unable to find valid certification path to requested target PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException JDK8 -> JDK10: PKIX path building failed: SunCertPathBuilderException: unable to find valid certification path to requested target PKIX path building failed: SunCertPathBuilderException: unable to find valid certification path to requested PKIX path building failed: unable to find valid certification path to requested target - imported CERT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM