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

huangapple go评论87阅读模式
英文:

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

问题

我在尝试连接到存储时遇到了这个异常。

当我查看论坛时,发现解决这个错误的方法是将证书添加到正在使用的 JVM 的信任存储文件中。

问题是我是新手,不太确定我应该如何做到这一点:我应该从哪里获取证书,如何添加它?

注:我注意到当我使用主方法运行它时,它可以正常工作!但当我设置一个调度程序来自动调用该方法时,我就会遇到这个异常。

非常感谢帮助。

异常是:

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)

以下是代码部分,无需翻译:

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");
        //...
    }
}
英文:

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?

N.B: 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)
	

答案1

得分: 2

尝试在进行此操作时禁用您的防病毒软件。这可能是问题的原因。

英文:

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

huangapple
  • 本文由 发表于 2020年8月5日 01:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63251729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定