JavaMail error + Unrecognized SSL message, plainte

2019-08-10 05:45发布

问题:

Using below code to read mails from a Zimbra mail server.

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();

But keep getting this error

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

Can anyone help on how to resolve this?

回答1:

Read this list of common mistakes and then clean up your code.

The Gmail example should work for you as well, although obviously you'll need to change the host name.



回答2:

This will help you bypass certificate process and get directly to ssl host

    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 
    }