简体   繁体   中英

Send e-mail with attachment on Android

I'm developing an application for Android that runs experiments and gets some statistics from Android devices. After getting the results, the app tries to send them by e-mail (with Intent.ACTION_SEND). However, I've been having a problem with the size of the raw message, so the message is compressed before being sent using GZip. I wouldn't like to create a file to be attached to message, as I would do if I used putExtra(Intent.EXTRA_STREAM, ...). It would be very straightforward if I could modify the message headers, but it seems that there is no way to do that. I've also tried to put headers information before the message, but the attachment hasn't been recognized by GMail client - Android embeds the whole message inside something like another attachment that has text/plain type. Is there a way to send a message with attachment without generating files?

private void sendResults(String title) {
    String body;

    try {

        body  = "Content-type: multipart/mixed; boundary=\"anexo\"\n\n";

        body += "--anexo\n";
        body += "Content-type: application/gzip; name=\"results.gz\" \n";
        body += "Content-disposition: attachment; filename=\"results.gz\" \n";
        body += "Content-Transfer-Encoding: base64 \n";
        body += Base64.encodeToString(ZipUtil.compress(results).getBytes(), Base64.DEFAULT) + "\n";

        body += "--anexo\n";
        body += "Content-type: text/plain; charset=us-ascii \n";
        body += "Results.\n";
        body += "--anexo--\n\n";

        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, body);
        String[] to = { "example_of_email@gmail.com" };
        sendIntent.putExtra(Intent.EXTRA_EMAIL, to);
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, "[dsp-benchmarking] "+title);
        sendIntent.setType("message/rfc822");
        startActivity(Intent.createChooser(sendIntent, "Send results"));

    } catch (IOException e) {
        Log.e("SEND_RESULTS", "Error: " + e.getMessage());
    }
}

Is it an option for you to use an e-mail scheduler app for this purpose? If so maybe you can use Email-Bot and configure it to send emails with attachments to a specific account in configurable intervals. You can also set the deletion of the attachment after the message was sent if it's required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM