javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found

0

Very good: I am developing an app that should send an email to a contact account. I tried to do as I put it on the internet but I can not get it to work, besides when I run it, the app does not fail me but it does not do what I need. The code is as follows:

StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();


StrictMode.setThreadPolicy(policy);
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";

// Sender's email ID needs to be mentioned
String from = "[email protected]";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.user", "mi_user");
properties.setProperty("mail.password", "mi_pass");

// Get the default Session object.
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication("mi_user", "mi_pass");
      }
});

try {
     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

     // Set Subject: header field
     message.setSubject("This is the Subject Line!");

     // Now set the actual message
     message.setText("This is actual message");

     // Send message
     Transport.send(message);
     Log.d("exito","exito");
} catch (MessagingException mex) {
     mex.printStackTrace();
}
    
asked by narut2011 03.12.2018 в 10:45
source

1 answer

0

to be an SSL certificate problem that is not authenticating you because it does not find the certificate path.

1) You could create a public key for the certificate, you have to install the openssl on your machine. Download the jar bcprov-jdk16-1.46.jar from link .

#!/bin/bash

 if [ -z $1 ]; then
  echo "Usage: cert2Android<CA cert PEM file>"
exit 1
 fi

 CACERT=$1
BCJAR=bcprov-jdk16-1.46.jar

TRUSTSTORE=mytruststore.bks
   ALIAS='openssl x509 -inform PEM -subject_hash -noout -in $CACERT'

  if [ -f $TRUSTSTORE ]; then
    rm $TRUSTSTORE || exit 1
  fi

echo "Adding certificate to $TRUSTSTORE..."
  keytool -import -v -trustcacerts -alias $ALIAS \
       -file $CACERT \
         -keystore $TRUSTSTORE -storetype BKS \
        -providerclass org.bouncycastle.jce.provider.BouncyCastleProvider \
        -providerpath $BCJAR \
        -storepass secret

  echo "" 
  echo "Added '$CACERT' with alias '$ALIAS' to $TRUSTSTORE..."

2) Copy the truststore mytruststore.bks file to res / raw in the trustore path of your project.

3) configures the SSL context of the connection.

............. okHttpClient = new OkHttpClient (); try {     KeyStore ksTrust = KeyStore.getInstance ("BKS");     InputStream instream = context.getResources (). OpenRawResource (R.raw.mytruststore);     ksTrust.load (instream, "secret" .toCharArray ());

// TrustManager decides which certificate authorities to use.
TrustManagerFactory tmf = TrustManagerFactory
    .getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ksTrust);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);

okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
   } catch (KeyStoreException | IOException | NoSuchAlgorithmException |      CertificateException | KeyManagementException e) {
e.printStackTrace();

} .................

You can find more information here: link

    
answered by 03.12.2018 в 14:46