Android JavaMail CertPathValidatorException

时间:2014-02-09 12:19:40

标签: java android javamail

我的发送邮件去年正在运行,但我想知道它为什么现在给我一个CertPathValidatorException

更新:我尝试使用Mkyong的教程(TLS)+覆盖socketfactory。但它给了我一个不同的例外。

筹码:

02-09 21:35:29.356    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ javax.mail.MessagingException: Could not convert socket to TLS;
02-09 21:35:29.356    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ nested exception is:
02-09 21:35:29.356    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ java.io.IOException: Exception in startTLS: host smtp.gmail.com, port 587; Exception: java.lang.NoSuchMethodException: getDefault []
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1339)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:420)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at javax.mail.Service.connect(Service.java:310)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sblive.connector.AsyncTasks.SendEmailAsyncTask.sendEmail(SendEmailAsyncTask.java:151)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sblive.connector.AsyncTasks.SendEmailAsyncTask.doInBackground(SendEmailAsyncTask.java:80)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sblive.connector.AsyncTasks.SendEmailAsyncTask.doInBackground(SendEmailAsyncTask.java:29)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
02-09 21:35:29.360    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ Caused by: java.io.IOException: Exception in startTLS: host smtp.gmail.com, port 587; Exception: java.lang.NoSuchMethodException: getDefault []
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:309)
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1334)
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ ... 15 more
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ Caused by: java.lang.NoSuchMethodException: getDefault []
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at java.lang.Class.getConstructorOrMethod(Class.java:423)
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at java.lang.Class.getMethod(Class.java:787)
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sun.mail.util.SocketFetcher.getSocketFactory(SocketFetcher.java:260)
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:292)
02-09 21:35:29.364    1861-1901/com.sblive.aufschoolbliz W/System.err﹕ ... 16 more

这是我的新sendEmail():

 private boolean sendEmail(String from, String to, String subject, String body){
        final String username = activity.getResources().getString(R.string.feedback_sender_email);
        final String password = activity.getResources().getString(R.string.feedback_sender_password);

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.socketFactory.class",
                "com.sblive.connector.MySSLSocketFactory");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }

1 个答案:

答案 0 :(得分:0)

您不信任服务器证书。首先要注意那里

Send Email in Service (without prompting user)

它对我有用。

其他解决方案:

信任服务器证书

将它导入您的信任库并使用javax.net.ssl.trustStore *系统属性指定信任stire(全局jre应该没问题)。

通过属性忽略服务器标识

 props.put("mail.transport.protocol", "smtps");
 props.put("mail.smtps.ssl.checkserveridentity", "false");
 props.put("mail.smtps.ssl.trust", "*");

how to ignore server cert error in javamail

重写SocketFactory以忽略服务器证书

使用

 props.put("mail.smtp.socketFactory.class", "MySSLSocketFactory ");

public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }
}