4

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[]{"recipient@example.com"});
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?

Jaydeepsinh Jadeja
  • 391
  • 2
  • 10
  • 20

4 Answers4

5

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.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
3
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"));
0

ACTION_SEND_MULTIPLE should be the action and then emailIntent.setType("text/plain"); followed by:

ArrayList<Uri> uris = new ArrayList<Uri>();
String[] filePaths = new String[] {"sdcard/sample.png", "sdcard/sample.png"};
for (String file : filePaths)
{
    File fileIn = new File(file);
    Uri u = Uri.fromFile(fileIn);
    uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);
Ren
  • 1,111
  • 5
  • 15
  • 24
Naveen R
  • 55
  • 1
  • 10
0

For sending an attachment with gmail:

  1. File should be on External storage device or created in External storage device

    • For that you need to add the following to Android Manifest <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    • get External path by

      String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();

    • Create a new file by

      File myfile=new File(pathname,filename);

    • Write to file based on whatever logic you are applying
    • Now the Intent

      Intent email=new Intent(android.content.Intent.ACTION_SEND);

      email.setType("plain/text");

    • Put Extras

      email.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(myfile)); email.putExtra(Intent.EXTRA_SUBJECT, "my email subject"); email.putExtra(Intent.EXTRA_TEXT, "my email text");

    • Start Activity startActivity(Intent.createChooser(email, "E-mail"));

abhi_novice
  • 51
  • 1
  • 4