56

I want to attach .vcf file with my mail and send through the mail. But the mail is received on the address without the attachment.I have used the below code but the code for this and i don't know where i am wrong.

try {      
  String filelocation="/mnt/sdcard/contacts_sid.vcf";      
  Intent intent = new Intent(Intent.ACTION_SENDTO);    
  intent.setType("text/plain");      
  intent.putExtra(Intent.EXTRA_SUBJECT, "");      
  intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));      
  intent.putExtra(Intent.EXTRA_TEXT, message);         
  intent.setData(Uri.parse("mailto:"));         
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

  activity.startActivity(intent);
  activity.finish();
  } catch(Exception e)  {
     System.out.println("is exception raises during sending mail"+e);
}
Phillip
  • 5,366
  • 10
  • 43
  • 62
Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68

5 Answers5

98

Use the below code to send a file within a email.

String filename="contacts_sid.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
Alaa
  • 115
  • 12
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • one look at my question...http://stackoverflow.com/questions/12798001/android-how-to-send-multiple-contacts-are-attached-in-single-vcf-file-and-send – NagarjunaReddy Oct 11 '12 at 06:37
  • 4
    You should not use "hard-coded" paths because they may change depending on the device. I suggest you changing the definition of filelocation to: File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename); Then define: Uri path = Uri.fromFile(filelocation); and put it in your intent: emailIntent .putExtra(Intent.EXTRA_STREAM, path); – Carlos Borau Nov 24 '15 at 07:52
  • 1
    emailIntent.putExtra(Intent.EXTRA_STREAM, filelocation) wouldn't attach the file for me, but using emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation)) as did Phillip worked fine. – andytrombone Jan 10 '16 at 01:18
  • 1
    How does this work if the file is not-yet saved? I have the image data as a Bitmap, but not a file. Can you provide the code to save the Bitmap first before we load it from file? – Alan Nelson Aug 25 '20 at 19:51
12

Folder_name is the name of the file in the Internal Storage of your phone. (ACTUALLY EXTERNAL_STORAGE). file_name is the name of the file you want to send.

private void ShareViaEmail(String folder_name, String file_name) {
    try {
        File root= Environment.getExternalStorageDirectory();
        String filelocation= root.getAbsolutePath() + folder_name + "/" + file_name;
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setType("text/plain");
        String message="File to be shared is " + file_name + ".";
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.setData(Uri.parse("mailto:xyz@gmail.com"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);
    } catch(Exception e)  {
        System.out.println("is exception raises during sending mail"+e);
    }
}
twenk11k
  • 557
  • 5
  • 17
Kshitij
  • 121
  • 1
  • 2
4

The example on the official Android site worked for me. All what is need it to add the

startActivity(Intent.createChooser(emailIntent , "Send email..."));

as done in Agarwal's answer

hamish
  • 397
  • 3
  • 3
  • In my case, its going to mail client but without attachment. toast displayed is "cant send empty file". my file is stored at `/data/data/com.example.app/files/temp.txt` and I'm passing it using `emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content:/"+filePath)); // filePath is /data/com.example.app/files/temp.txt ` – kAmol Sep 13 '15 at 18:00
  • 1
    You can't send file because it is in your app's cache directory, and only your app can read from that directory. You should use another directory, like Environment.getExternalStorageDirectory(). – Borzh Sep 28 '15 at 18:17
  • Used Environment.getExternalStorageDirectory(), verified that path was valid and that file had good data.... but still get the same error message (?). – CESDewar Mar 16 '16 at 19:14
3

SENDTO doesnt support attachment. I have added my answer using Provider to read the file information. Its in Kotlin.

fun shareFile(context: Context, filePath: File?, fileShareInfo: FileShareInfo) {

    val intentFileShare = Intent(Intent.ACTION_SEND)

    if (filePath!!.exists()) {
        intentFileShare.type = fileShareInfo.fileType
        val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath)
        intentFileShare.putExtra(Intent.EXTRA_STREAM, uri)
        fileShareInfo.recipients?.let {
            intentFileShare.putExtra(Intent.EXTRA_EMAIL, fileShareInfo.recipients)
        }
        intentFileShare.putExtra(Intent.EXTRA_SUBJECT, fileShareInfo.shareSubjectText)
        fileShareInfo.shareExtraText?.let {
            intentFileShare.putExtra(Intent.EXTRA_TEXT, AppViewUtil.fromHtml(fileShareInfo.shareExtraText!!))
        }
        try {
            ContextCompat.startActivity(context, Intent.createChooser(intentFileShare, fileShareInfo.shareTitle), null)
        } catch (e: ActivityNotFoundException) {
            Toast.makeText(context, context.getString(R.string.sharing_no_app_found), Toast.LENGTH_LONG).show()
        }

    }
}
Deep P
  • 557
  • 5
  • 12
1

I have written an extension in Kotlin for sending emails with multiple attachments. I hope it is useful for someone.

fun AppCompatActivity.sendEmail(subject: String, content: String, email: String, fileAttachments: List<String> = emptyList()) {

    val emailIntent = Intent(Intent.ACTION_SEND_MULTIPLE).apply {
        type = "text/html"
        putExtra(Intent.EXTRA_SUBJECT, subject)
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        putExtra(Intent.EXTRA_TEXT, content)
        putExtra(Intent.EXTRA_EMAIL, arrayOf(email))

        // Configure attachments
        val attachments = fileAttachments.map { File(it) }.filter { it.exists() && !it.isDirectory }.map {
            FileProvider.getUriForFile(baseContext, "${BuildConfig.APPLICATION_ID}.fileprovider", it)
        }.toList()

        if(attachments.isNotEmpty())
            putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(attachments))
    }

    if (emailIntent.resolveActivity(packageManager) != null)
        startActivity(Intent.createChooser(emailIntent, "Chooser Mail Client"))
}
Sergio Sánchez Sánchez
  • 1,694
  • 3
  • 28
  • 48