JavaMail的错误+无法识别的SSL消息,纯文本连接?(JavaMail error + Unr

2019-10-17 05:18发布

使用下面的代码读取从Zimbra的邮件服务器的邮件。

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

Properties pop3Props = new Properties();

pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.auth", "true");
pop3Props.setProperty("mail.pop3.port",  "110");
pop3Props.setProperty("mail.pop3.socketFactory.port", "110");

URLName url = new URLName("pop3", host, 110, "",username, password);

Session session = Session.getInstance(pop3Props, null);
Store store = new POP3SSLStore(session, url);
store.connect();

但是要得到这个错误

javax.mail.MessagingException: Connect failed;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at VerifyMail.readPOP3(VerifyMail.java:177)
    at VerifyMail.main(VerifyMail.java:20)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
    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 java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at java.io.DataInputStream.readLine(Unknown Source)
    at com.sun.mail.pop3.Protocol.simpleCommand(Protocol.java:347)
    at com.sun.mail.pop3.Protocol.<init>(Protocol.java:91)
    at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:201)
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:144)
    ... 5 more

任何人都可以就如何解决这方面的帮助?

Answer 1:

阅读的常见错误列表 ,然后清理你的代码。

在Gmail的例子应该为你工作为好,虽然很明显你需要更改主机名。



Answer 2:

这将帮助你绕过认证过程,并直接得到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 
    }


文章来源: JavaMail error + Unrecognized SSL message, plaintext connection?