Java Mail 从交换服务器获取带有 pop3 的邮件 => 线程 "main"javax.mail.MessagingException 中的异常

标签 java ssl jakarta-mail

以下代码从交换电子邮件帐户获取邮件:

public class TicketMain {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        final Properties props = new Properties();

        //XTrustProvider.install();

       // System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", true);

        props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.pop3.socketFactory.fallback", "false");
        props.setProperty( "mail.pop3.host", "10.30.0.103" );
        props.setProperty( "mail.pop3.user", "xxxx");
        props.setProperty( "mail.pop3.password", "xxxx!");
        props.setProperty( "mail.pop3.ssl.enable", "true");
        props.setProperty( "mail.pop3.port", "445" );
        props.setProperty( "mail.pop3.auth", "true" );      
        props.setProperty("mail.pop3.starttls.enable", "false"); 
       /* props.setProperty( "mail.pop3.starttls.enable", "true" );
        props.setProperty( "mail.pop3.starttls.required", "true" );*/

        Session session  = Session.getInstance(props);
        session.setDebug(true);



        Store store = session.getStore("pop3");
        store.connect("xxxx", "xxxx!");         
        Folder folder = store.getDefaultFolder();
        folder.open(Folder.READ_ONLY);
        Message message[] = folder.getMessages();
        for ( int i = 0; i < message.length; i++ )
        {
          Message m = message[i];
              System.out.println( "-------------------------\nNachricht: " + i );
              System.out.println( "From: " + Arrays.toString(m.getFrom()) );
              System.out.println( "Topic: " + m.getSubject() );   


          if ( m.isMimeType("text/plain") )
            System.out.println( m.getContent() );
        }
        folder.close( false );
        store.close();

    }

}

执行此应用程序时出现以下错误:

DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "10.30.0.103", port 445, isSSL true
keyStore is : 
keyStore type is : jks
keyStore provider is : 
init keystore
init keymanager of type SunX509
trustStore is: C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\security\cacerts
trustStore type is : jks
trustStore provider is : 
init truststore
trigger seeding of SecureRandom
done seeding SecureRandom
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA
Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA
Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256
Allow unsafe renegotiation: true
Allow legacy hello messages: true
Is initial handshake: true
Is secure renegotiation: false
%% No cached client session
*** ClientHello, TLSv1
RandomCookie:  GMT: 1389798620 bytes = { 115, 122, 157, 36, 180, 32, 127, 18, 33, 140, 18, 51, 218, 143, 189, 173, 30, 232, 215, 2, 114, 58, 144, 193, 229, 138, 82, 162 }
Session ID:  {}
Cipher Suites: [TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_RC4_128_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_MD5, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA]
Compression Methods:  { 0 }
Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1}
Extension ec_point_formats, formats: [uncompressed]
***
main, WRITE: TLSv1 Handshake, length = 149
main, handling exception: java.net.SocketException: Software caused connection abort: recv failed
main, SEND TLSv1 ALERT:  fatal, description = unexpected_message
main, WRITE: TLSv1 Alert, length = 2
main, Exception sending alert: java.net.SocketException: Software caused connection abort: socket write error
main, called closeSocket()
Exception in thread "main" javax.mail.MessagingException: Connect failed;
  nested exception is:
    java.net.SocketException: Software caused connection abort: recv failed
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:213)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at javax.mail.Service.connect(Service.java:265)
    at com.technisat.polarion.ticket.TicketMain.main(TicketMain.java:42)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:312)
    at sun.security.ssl.InputRecord.read(InputRecord.java:350)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1294)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1321)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1305)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:543)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:348)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:215)
    at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
    at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:264)
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)
    ... 4 more

SSL 证书已导入到 java keystore 中。

这里有什么问题吗?

telnet 主机端口 正在从客户端工作。

最佳答案

您正在尝试建立到纯文本 POP3 端口而非 POP3S 端口的 SSL/TLS 连接。

因此,如果您想使用 SSL/TLS,您必须启用 starttls 但禁用 ssl。

    props.setProperty("mail.pop3.ssl.enable", "false");
    props.setProperty("mail.pop3.starttls.enable", "true"); 
    props.setProperty("mail.pop3.starttls.required", "true");

关于Java Mail 从交换服务器获取带有 pop3 的邮件 => 线程 "main"javax.mail.MessagingException 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25017050/

相关文章:

java - 如何使用 MS Exchange 服务器发送电子邮件

java - JHipster 邮件服务 - javax.mail.SendFailedException : No recipient addresses

java - 按子列表的值对 LIst 列表进行排序

java - 以数组形式返回 int 的质因数作为输入

ios - UIWebView 加载第一个 HTTPS 页面正常,但在打开链接时失败

r - 在 Mac 10.11.3 上使用 RPostgreSQL 将 R 连接到 Redshift

java - 使用 Spring boot CrudRepository 过滤数据

java - 在多个项目中使用自定义包

ssl - 使用 Curl、SSL 和 cookie 登录

Java 应用程序无法从命令行发送电子邮件,从 Eclipse 可以