2014-02-26 113 views
1

我想通過訪問POP3服務器來下載郵件在本地環境中配置了SSL,但是我們失敗了。JavaMail POP3 over SSL連接失敗;

連接時需要採取哪些步驟?

異常

javax.mail.MessagingException: Connect failed; 
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 

示例代碼

Properties props = new Properties(); 
final String HOST = "host"; 
final String PROTOCOL = "pop3"; 
final String PORT = "995"; 
String username = "user"; 
String pass = "password"; 
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); 
props.setProperty("mail.pop3.socketFactory.fallback", "false"); 
props.setProperty("mail.pop3.port", PORT); 
props.setProperty("mail.pop3.socketFactory.port", PORT); 
URLName urln = new URLName(PROTOCOL, HOST, Integer.parseInt(PORT), null, username, pass); 

Session session = Session.getInstance(props, null); 
Store store = session.getStore(urln); 

store.connect(); 

回答

1

設置屬性mail.pop3.ssl.trust

props.setProperty("mail.pop3.ssl.trust", "*"); // Trust all Servers 

你也應該能夠只將其設置爲您的具體服務器,用*之前測試雖然

props.setProperty("mail.pop3.ssl.trust", HOST); 
+0

這幾乎是正確的。有關詳細信息,請參見[本FAQ條目](http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert)。請參閱此[常見錯誤列表](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes)以清理您的代碼。 –

+0

使用正確的屬性名稱編輯我的帖子 – user432