Send pdf by email from android

1

I have an app that allows me to send a PDF by email, either hotmail or gmail. When I made the app I did it all in activities and it still works correctly but now I decided to create a version in Fragments and the method to send the pdfs stopped working.

I'll leave the code below

Method in the Fragment FragmentoVentas

sqLiteDb.vecFact [5]: Email sent to edtEmail.getText (). toString (): Email to which I sent
> sqLiteDb.vecFact [6]: Password of the email that sends file: pdf path

  private void enviarPdf(String archivo){ //archivo es la ruta del pdf que esta en el dispositivo y que se creo 
    TareaAsincronaEmail tMail = new TareaAsincronaEmail(getActivity(),"envio");
    tMail.execute(sqLiteDb.vecFact[5],edtEmail.getText().toString(),sqLiteDb.vecFact[6],archivo);
    try {
        Toast.makeText(getContext(),tMail.get(),Toast.LENGTH_SHORT).show();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

Mail asynchronous task

 @Override
    protected String doInBackground(String... params){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Properties props = new Properties();
        props.setProperty("mail.smtp.host","smtp.gmail.com");
        props.setProperty("mail.smtp.starttls.enable", "true");
        props.setProperty("mail.smtp.port","587");
        props.setProperty("mail.smtp.user", params[0]);
        props.setProperty("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(params[0]));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(params[1]));
            message.setSubject("Factura...");
            message.setText("Se le hace envio de su factura"); //el Logcat me marca el error en esta linea

            Multipart multipart = new MimeMultipart();
            MimeBodyPart mimeBodyPartAdjunto = new MimeBodyPart();
            mimeBodyPartAdjunto.setFileName("Factura_de_venta_sistevar.pdf");
            mimeBodyPartAdjunto.attachFile(params[3]);
            multipart.addBodyPart(mimeBodyPartAdjunto);

            message = new MimeMessage(session);
            message.setFrom(new InternetAddress(params[0]));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(params[1]));
            message.setSubject("Gracias por comprar en sistevar");
            message.setContent(multipart);

            Transport t = session.getTransport("smtp");
            t.connect(params[0],params[2]);
            t.sendMessage(message,message.getAllRecipients());
            t.close();
            return "Mensaje enviado";
        } catch (MessagingException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

Error marked by Logcat

 Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.activation.DataHandler" on path: DexPathList[[zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/base.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_dependencies_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_0_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_1_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_2_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_3_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_4_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_5_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_6_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_7_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_8_apk.apk", zip file "/data/app/desarrollos.lfpu.com.pos_connect-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/desarrollos.lfpu.com.pos_connect-2/lib/arm64, /vendor/lib64, /system/lib64]]

All this code is the same that I use in the app where it works in activities but in this one that is with fragments it hangs up when sending the mail.

    
asked by Fernando Urrego 10.05.2018 в 18:54
source

1 answer

-1

If you check the error:

  

Caused by: java.lang.ClassNotFoundException: Did not find class   "javax.activation.DataHandler" on path:

indicates that you are using a class of javax in your project called DataHandler , use the javax classes on Android is not supported since they are not included in the Android library, for that reason the class does not exist and you are getting this error.

Investigating a bit more of your error I found this question in SO , where you indicate that you use Javamail for your purpose.

  

There are three libraries that you must include in your application: mail.jar ,    activation.jar and additionnal.jar (sic). It seems that something is missing   which depends on the activation library, and this could be due to   that is not using the Android port of this library. I have   successfully used the version of javamail for Android in a   project and it works very well.

    
answered by 10.05.2018 в 22:21