2011-02-04 86 views
18

當我使用imaps連接到我的imap服務器時,它失敗。如何忽略javamail中的服務器證書錯誤

你能告訴我如何在JavaMail的

Exception in thread "main" 
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:665) 
    at javax.mail.Service.connect(Service.java:295) 
    at javax.mail.Service.connect(Service.java:176) 
    at App20110204.main(App20110204.java:31) 
Caused by: 
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.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1623) 
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:198) 
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:192) 
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1074) 
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:128) 
    at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:529) 
    at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:465) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:884) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1120) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1147) 
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1131) 
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) 
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:113) 
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:110) 
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:632) 
    ... 3 more Caused by: 
sun.security.validator.ValidatorException: 
PKIX path building failed: 
sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification 
path to requested target at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:294) 
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:200) 
    at sun.security.validator.Validator.validate(Validator.java:218) 
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126) 
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209) 
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249) 
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1053) 
    ... 15 more Caused by: 
sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification 
path to requested target at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174) 
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238) 
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:289) 
    ... 21 more 

忽略服務器證書錯誤,我的源代碼

Properties prop = new Properties(); 
prop.put("mail.imap.ssl.checkserveridentity", "false"); 
prop.put("mail.imap.ssl.trust", "*"); 

Session session = Session.getDefaultInstance(prop); 
Store store = session.getStore("imaps"); 
store.connect("mail.xxx.com", "xxxx", "[email protected]"); 
System.out.println(store.getFolder("INBOX").getMessageCount()); 
+0

可能的重複http://stackoverflow.com/questions/4062307/pkix-path-building-failed-unable-to-find-valid-certification-path-to-requested-t – Raghuram 2011-02-04 05:49:58

回答

43

使用prop.put("mail.imaps.ssl.trust", "*");,因爲你正在使用imaps店。

smtp你可以試試: prop.put("mail.smtp.ssl.trust", "*");

4

如果您使用的是javamail 1.4.2+,則可以使用一個套接字工廠來忽略服務器證書。

MailSSLSocketFactory socketFactory= new MailSSLSocketFactory(); 
socketFactory.setTrustAllHosts(true); 
prop.put("mail.imap.ssl.socketFactory", socketFactory); 
+6

爲什麼要打擾SSL,如果你無論如何都願意相信所有的主機......不要在生產中使用它。 – Bruno 2012-01-05 09:59:39

+0

我同意我們不應該在生產中使用它。這通常用於自簽名證書。連接到測試使用自簽名證書的imaps服務器時,我遇到了同樣的問題。這爲我修好了。 – 2012-01-05 23:52:24

28

不要忽略證書驗證錯誤(除非可能在測試環境中):這違背了使用SSL/TLS的要點。例如,如果您知道您信任該服務器證書,請將其導入您的信任存儲(JRE的全局信任存儲或您使用javax.net.ssl.trustStore*系統屬性指定的本地信任存儲)。

3
 Properties propsSSL = new Properties(); 
    propsSSL.put("mail.transport.protocol", "smtps"); 
    propsSSL.put("mail.smtps.host", "hostname"); 
    propsSSL.put("mail.smtps.auth", "true"); 
    propsSSL.put("mail.smtps.ssl.checkserveridentity", "false"); 
    propsSSL.put("mail.smtps.ssl.trust", "*"); 

上述變化將修復javax.mail.MessagingException: Could not connect to SMTP host: hostname, port: 465;的嵌套異常

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors 
exception 
1

我同樣的問題,使用

MailSSLSocketFactory socketFactory= new MailSSLSocketFactory(); 
socketFactory.setTrustAllHosts(true); 
prop.put("mail.pop3s.ssl.socketFactory", socketFactory); 

com.sun.mail.util.MailSSLSocketFactory

其作品!!

0
Properties pr = new Properties(); 
    MailSSLSocketFactory socketFactory= new MailSSLSocketFactory(); 
    socketFactory.setTrustAllHosts(true); 
    pr.put("mail.pop3s.ssl.socketFactory", socketFactory); 
    Session ses = Session.getInstance(pr); 
    ses.setDebug(true); 
    URLName url = new URLName("pop3s://username:[email protected]:posrt"); 
    Store store = ses.getStore(url.getProtocol()); 
    store.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword()); 
    Folder inbox = store.getFolder("INBOX"); 
    inbox.open(Folder.READ_ONLY); 
    try { 
     int i = inbox.getMessageCount(); 
     com.sun.mail.pop3.POP3Message mes; 
     while (i > 0) { 
      mes = (com.sun.mail.pop3.POP3Message) inbox.getMessage(i); 
      System.out.println(mes.getContentID()); 
      i--; 
     } 
    } finally { 
     inbox.close(false); 
     store.close(); 
    } 

DEBUG:setDebug:JavaMail的版本1.4.5
Exchange Server 2010的
PlainTextLogin
http://technet.microsoft.com/ru-ru/library/bb124498(v=exchg.141).aspx

0

這將幫助你繞過認證過程,並直接得到SSL主機

MailSSLSocketFactory sf = null; 
try 
{ 
    sf = new MailSSLSocketFactory(); 
} 
catch (GeneralSecurityException e) 
{ 
    e.printStackTrace(); 
} 
     sf.setTrustAllHosts(true); 

Properties pop3Props = new Properties(); 
pop3Props.setProperty("mail.pop3.ssl.enable", "true"); 
pop3Props.setProperty("mail.protocol.ssl.trust", "pop3.live.com"); 
pop3Props.put("mail.pop3s.ssl.socketFactory", sf); 
pop3Props.setProperty("mail.pop3s.port", "995"); 

Session session = Session.getInstance(pop3Props); 

try 
{ 
/* Get a Store object*/ 
    Store store = session.getStore("pop3s"); 
//process further activity 
} 
2

我認爲@布魯諾對admoni是正確的SH你不要盲目地相信所有服務器的黑客setTrustAllHosts(true)

在他們演示如何將開發郵件主機添加到信任列表而不迫使您的應用程序不安全相信全世界的docs at Oracle

MailSSLSocketFactory sf = new MailSSLSocketFactory(); 
sf.setTrustedHosts(new String[] { "my-server" }); 
props.put("mail.smtp.ssl.enable", "true"); 
// also use following for additional safety 
props.put("mail.smtp.ssl.checkserveridentity", "true"); 
props.put("mail.smtp.ssl.socketFactory", sf); 
0

我是同樣的問題。

MailSSLSocketFactory socketFactory = new MailSSLSocketFactory(); socketFactory。setTrustedHosts(new String [] {「my-server」});

socketFactory.setTrustAllHosts(true); props.put(「mail.smtps.socketFactory」,socketFactory);

它的作品!

1

如果問題仍然存在於Java 6中,那麼解決方案很簡單。它與Java 7的發佈一樣簡單。在machine.java 7中安裝java 7具有能夠忽略證書認證的證書文件。

複製 「的cacerts」 文件從下面的Java 7的目錄

C:\ Program Files文件\的Java \ jdk1.7.0_79 \ JRE \ lib \ security中

並粘貼在

C:\ Program Files文件\的Java \ jdk1.6.0 \ JRE \ lib \ security中

現在證書認證問題將得到解決。