how to ignore server cert error in javamail

后端 未结 10 1316
长发绾君心
长发绾君心 2020-12-05 07:12

when i connect to my imap server using imaps,it failes.

can you tell me how to ignore server cert error in javamail

Exception in thread \"main\"
java         


        
相关标签:
10条回答
  • 2020-12-05 07:49
        Properties pr = new Properties();
        MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
        socketFactory.setTrustAllHosts(true);
        pr.put("mail.pop3s.ssl.socketFactory", socketFactory);
        Session ses = Session.getInstance(pr);
        ses.setDebug(true);
        URLName url =  new URLName("pop3s://username:password@host:posrt");
        Store store = ses.getStore(url.getProtocol());
        store.connect(url.getHost(), url.getPort(), url.getUsername(), url.getPassword());
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        try {
            int i = inbox.getMessageCount();
            com.sun.mail.pop3.POP3Message mes;
            while (i > 0) {
                mes = (com.sun.mail.pop3.POP3Message) inbox.getMessage(i);
                System.out.println(mes.getContentID());
                i--;
            }
        } finally {
            inbox.close(false);
            store.close();
        }
    

    DEBUG: setDebug: JavaMail version 1.4.5
    Exchange server 2010
    PlainTextLogin
    http://technet.microsoft.com/ru-ru/library/bb124498(v=exchg.141).aspx

    0 讨论(0)
  • 2020-12-05 07:51

    Don't ignore certificate verification errors (unless perhaps in a test environment): this defeats the point of using SSL/TLS.

    Instead, if you know you trust that server certificate, import it in your trust store (either the global trust store of the JRE or a local one that you specify with the javax.net.ssl.trustStore* system properties, for example).

    0 讨论(0)
  • 2020-12-05 07:55

    I was the same issue, using

    MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
    socketFactory.setTrustAllHosts(true);
    prop.put("mail.pop3s.ssl.socketFactory", socketFactory);
    

    com.sun.mail.util.MailSSLSocketFactory

    it's works!!

    0 讨论(0)
  • 2020-12-05 07:56

    I think @Bruno is correct to admonish you not to blindly trust all servers with the hack setTrustAllHosts(true)

    In the docs at Oracle they show how to add your dev mail host to the trusted list without forcing your app to insecurely trust the whole world:

    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustedHosts(new String[] { "my-server" });
    props.put("mail.smtp.ssl.enable", "true");
    // also use following for additional safety
    props.put("mail.smtp.ssl.checkserveridentity", "true");
    props.put("mail.smtp.ssl.socketFactory", sf);
    
    0 讨论(0)
  • 2020-12-05 07:57

    If you are using javamail 1.4.2+, there is a socket factory you can use to ignore server certificate.

    MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
    socketFactory.setTrustAllHosts(true);
    prop.put("mail.imap.ssl.socketFactory", socketFactory);
    
    0 讨论(0)
  • 2020-12-05 08:00

    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 
    }
    
    0 讨论(0)
提交回复
热议问题