0

My send email was working last year but I'm wondering why it now gives me a CertPathValidatorException

Update: I tried to use Mkyong's tutorial (TLS) + Overriding socketfactory. However it gives me a different exception.

The stack:

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

Here is my new 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;
        }
dgzz
  • 2,929
  • 6
  • 34
  • 56

1 Answers1

0

You are not trusting the server certificate. Firstly get an eye there

Send Email in Service (without prompting user)

it works for me.

Other solutions:

Trust server certificate

Import it in your trust store and you specify with the javax.net.ssl.trustStore* system properties the trust stire (global jre shoul be ok).

Ignore server identity through properties

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

or how to ignore server cert error in javamail

Overriding SocketFactory to Ignore Server certificate

use

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

and

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();
    }
}
Community
  • 1
  • 1
venergiac
  • 7,469
  • 2
  • 48
  • 70
  • i tried to use "Overriding SocketFactory" method it gives me an exception: java.lang.NoSuchMethodException: getDefault [] – dgzz Feb 09 '14 at 14:13
  • I also tried to ignore server identity by properties but it gives me the same error – dgzz Feb 09 '14 at 14:35
  • Daniel, did yout tried my code. It works for me? What is "org.apache.harmony.xnet.provider.jsse"?? – venergiac Feb 09 '14 at 16:21
  • no, it is a regression... please write a log on "checkServerTrusted" and remove unnecessary linrary – venergiac Feb 09 '14 at 21:56
  • ok then it is not calling the socket factory fid you "inject" the socket factory props.put("mail.smtp.socketFactory.class", mySSLSocketFactory); ? – venergiac Feb 10 '14 at 15:02