Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attachment is not coming in mail programmatically

I am attaching a TEXT file to Email with code :

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
                                    "[email protected]", null));

    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report");

    emailIntent.putExtra(Intent.EXTRA_TEXT, prepareBodyMail());
    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, "/MyFolder/report.txt");

    Uri uri = Uri.fromFile(file);
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));

This code works perfectly with Gmail, Email and other apps

But this is not attaching file with INBOX application by Google

Only Body and subject are coming without any attachment

I have reported this problem to Google Groups at Inbox Problem

Can anybody help what I am missing in code?

like image 353
Kushal Avatar asked Sep 06 '25 08:09

Kushal


1 Answers

In Kotlin

fun Context.shareAttachmentsToMail(mailId: String?, uri: Uri, subject: String, bodyMessage: String){
  
    val selectorIntent = Intent(ACTION_SENDTO).apply {
        data = Uri.parse("mailto:")
    }
    val emailIntent = Intent(ACTION_SEND).apply {
        putExtra(Intent.EXTRA_EMAIL, arrayOf(mailId))
        putExtra(Intent.EXTRA_SUBJECT, subject)
        putExtra(Intent.EXTRA_TEXT, bodyMsg)
        putExtra(Intent.EXTRA_STREAM, uri)
        selector = selectorIntent
    }
    try {
        startActivity(Intent.createChooser(emailIntent, ""))
    } catch (e: ActivityNotFoundException) {
        // Application not found
    }
}

Call this extension function like this:

requireContext().shareAttachmentsToMail(mailId = mailId, uri = uri, subject = "your subject", bodyMessage = "your body message")
like image 176
Tippu Fisal Sheriff Avatar answered Sep 09 '25 13:09

Tippu Fisal Sheriff