Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Attach files with sending mail in android application?

Tags:

android

email

I am sending mail through my application. For that I am using following code.

    Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

It just work fine but i want to attach an xml file with it. Is it possible? How?

like image 277
Jaydeepsinh Jadeja Avatar asked Feb 22 '23 07:02

Jaydeepsinh Jadeja


2 Answers

There are lots of similar questions asked with perfect solution in Stack Overflow already.

You can have look to few : here and here and here

Solution is to use with email intent : one more putExtra with Key-Extra_Stream and Value-uri to file.

And please go through the FAQ to undersatand How to better benifit from the site.

like image 143
MKJParekh Avatar answered Mar 03 '23 04:03

MKJParekh


String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();
String filename="/MyFiles/mysdfile.txt";
File file=new File(pathname, filename);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Your email id"));
like image 42
user2662974 Avatar answered Mar 03 '23 05:03

user2662974