2016-05-30 32 views
0

JavaMailライブラリを使用してGmailからPOP3経由でメールを取得しようとしましたが、SSLハンドシェークエラーが発生します。 SSLハンドシェイクをどうやって行うのですか?JavaMail経由でgmailに接続しようとしたときにSSLハンドシェイクエラーが発生しました。

は、任意の助けいただければ幸いです

コード:

import java.util.Properties; 
import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.NoSuchProviderException; 
import javax.mail.Session; 
import javax.mail.Store; 

public class CheckingMails { 

public static void check(String host, String storeType, String user, 
    String password) 
{ 
    try { 


    Properties properties = new Properties(); 

    properties.put("mail.pop3.host", host); 
    properties.put("mail.pop3.port", "995"); 
    properties.put("mail.pop3.starttls.enable", "true"); 
    Session emailSession = Session.getDefaultInstance(properties); 


    Store store = emailSession.getStore("pop3s"); 

    store.connect(host, user, password); 


    Folder emailFolder = store.getFolder("INBOX"); 
    emailFolder.open(Folder.READ_ONLY); 


    Message[] messages = emailFolder.getMessages(); 
    System.out.println("messages.length---" + messages.length); 

    for (int i = 0, n = messages.length; i < n; i++) { 
    Message message = messages[i]; 
    System.out.println("---------------------------------"); 
    System.out.println("Email Number " + (i + 1)); 
    System.out.println("Subject: " + message.getSubject()); 
    System.out.println("From: " + message.getFrom()[0]); 
    System.out.println("Text: " + message.getContent().toString()); 

    } 

    //close the store and folder objects 
    emailFolder.close(false); 
    store.close(); 

    } catch (NoSuchProviderException e) { 
    e.printStackTrace(); 
    } catch (MessagingException e) { 
    e.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    }} public static void main(String[] args) { 

    String host = "pop.gmail.com"; 
    String mailStoreType = "pop3"; 
    String username = "[email protected]"; 
    String password = "xxx"; 

    check(host, mailStoreType, username, password);}} 

出力:誰もが後でこの質問につまずくなら、私がやったように

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 
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:213) 
    at javax.mail.Service.connect(Service.java:366) 
    at javax.mail.Service.connect(Service.java:246) 
    at reademail.CheckingMails.check(CheckingMails.java:30) 
    at reademail.CheckingMails.main(CheckingMails.java:70) 
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 sun.security.ssl.Alerts.getSSLException(Alerts.java:192) 
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) 
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478) 
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212) 
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) 
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914) 
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375) 
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:598) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:372) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) 
    at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112) 
    at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:265) 
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207) 
    ... 4 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:387) 
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) 
    at sun.security.validator.Validator.validate(Validator.java:260) 
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) 
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) 
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460) 
    ... 17 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145) 
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131) 
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) 
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382) 
    ... 23 more 
+0

トラストストアが証明書を信頼しません。カスタムトラストストアを使用していますか? Javaに同梱されているものは、それを信頼する必要があります。 – EJP

+1

[this JavaMail FAQ entry](http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert)の一般的な原因を参照してください。 –

答えて

0

、解決策は次のとおりです。

properties.put("mail.pop3.host", host); 
properties.put("mail.pop3.port", "995"); 
properties.put("mail.pop3.starttls.enable", "true"); 
props.put("mail.pop3s.ssl.trust", host); // add this line 
Session emailSession = Session.getDefaultInstance(properties); 

接続しようとしているホストを信頼するために4行目を追加できます。 ホストのパラメータを "*"に変更しないでください。この問題の回避する

その他の方法あなたがする必要がある、ということを行うために、あなたのjavaにのcacerts

をSSL証明書を追加します

Firefoxを使用して
  1. 、「.CRTを」エクスポート(gmail.comなどの)ページからファイルをダウンロードするには、アドレスバーの緑色の南京錠をクリックします。
  2. Windowsでは、管理者権限でCMDを開きます。
  3. は、以下の(GoogleのGmailの証明書のEX)完了

    keytool -import -trustcacerts -alias googlemail -file mailgooglecom.crt -noprompt -keystore cacerts

を実行します。

関連する問題