How to attach .txt in an email (Android Studio)?

2

I am trying to attach a ".txt" file in a message.  When I run the application, and in principle it is when the message should be sent, nothing is sent, I do not know if it is because it does not take the file well or because reason. I want the message to be sent automatically and the user does not have to interact with the application.

The code is as follows:

    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.port", "465");
    properties.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {

                    return new PasswordAuthentication(user, pass);
                }
            });

        try {


            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("correoOrigen"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("correoDestino"));
            message.setSubject("asuntoEjemplo");

            MimeBodyPart adjunto = new MimeBodyPart();
            adjunto.setDataHandler(new DataHandler(new FileDataSource(new File(Environment.getDownloadCacheDirectory().getAbsolutePath() + "/ejemplo.txt"))));

            adjunto.setContent(adjunto,"text/plain");

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(adjunto);

            message.setContent(multipart);

            Transport.send(message);

        } catch (MessagingException e) {
            Log.d("MailJob", e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

The file I want to send is in the device downloads folder, and its name is "example.txt". I've already been looking at several threads on the internet that talk about the same topic but I can not get it to work even if I take them as a reference, I think the problem is when it comes to taking the file, which I will have misplaced the address or something similar.

    
asked by Jorge1997 10.11.2017 в 22:12
source

1 answer

1

To attach files, it is necessary that the file is on the disk. Checking your code the file you want to send is located at;

Environment.getDownloadCacheDirectory().getAbsolutePath() + "/ejemplo.txt";

Therefore in this way you can attach the file to send it via email:

        String archivoTXT = "ejemplo.txt";

        //Obtiene la Uri del recurso.
        Uri uri = Uri.fromFile(new File(Environment.getDownloadCacheDirectory().getAbsolutePath(), archivoTXT ));
        //Crea intent para enviar el email.
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("application/pdf");
        //Agrega email o emails de destinatario.
        i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
        i.putExtra(Intent.EXTRA_SUBJECT, "Envio de archivo .TXT.");
        i.putExtra(Intent.EXTRA_TEXT, "Hola te envío un archivo .TXT!"); 
        i.putExtra(Intent.EXTRA_STREAM,  uri);
        startActivity(Intent.createChooser(i, "Enviar e-mail mediante:"));
    
answered by 10.11.2017 / 22:23
source