如何使用imap xoauth获取电子邮件

时间:2012-08-03 14:27:07

标签: java oauth imap

如何通过使用xoauth验证imap从gmail检索邮件。我已经尝试了为xoauthAuthenticator.java,xoauthprotocol.java提供Gmail数据api的代码, xoauthsaslclient.java和xoauthsaslclienfactory.java

但是我得到了这样的错误

Exception in thread "main" javax.mail.MessagingException: Remote host closed connection during handshake;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:479)
    at javax.mail.Service.connect(Service.java:275)
    at XoauthAuthenticator.connectToImap(XoauthAuthenticator.java:80)
    at XoauthAuthenticator.main(XoauthAuthenticator.java:114)
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at com.sun.mail.iap.ResponseInputStream.read0(ResponseInputStream.java:81)
    at com.sun.mail.iap.ResponseInputStream.readResponse(ResponseInputStream.java:67)
    at com.sun.mail.iap.Response.<init>(Response.java:83)
    at com.sun.mail.imap.protocol.IMAPResponse.<init>(IMAPResponse.java:48)
    at com.sun.mail.imap.protocol.IMAPResponse.readResponse(IMAPResponse.java:122)
    at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:230)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:91)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:87)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:446)
    ... 3 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
    ... 19 more

可能是什么原因?

1 个答案:

答案 0 :(得分:0)

当SSLhandler不信任来自主机服务器的证书时,抛出SSLHandshakeException。您需要将服务器的SSL证书添加到密钥库。 Here is a good walk through of the problem以及如何修复它。