Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an email with a file attachment in Android

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); } 
like image 790
Naresh Sharma Avatar asked Apr 02 '12 10:04

Naresh Sharma


People also ask

How do I attach a photo to an email on Android?

Open the photo app on your phone. In the photo gallery, long press each photo you would like to share until it shows as selected. A dot (or a check mark) will appear in the corner. Tap the Share icon and choose your desired email.


2 Answers

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[] = {"[email protected]"}; 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...")); 
like image 197
Shankar Agarwal Avatar answered Oct 02 '22 14:10

Shankar Agarwal


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:[email protected]"));         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);          startActivity(intent);     } catch(Exception e)  {         System.out.println("is exception raises during sending mail"+e);     } } 
like image 34
Kshitij Avatar answered Oct 02 '22 13:10

Kshitij