Jaydeepsinh Jadeja
Jaydeepsinh Jadeja

Reputation: 391

How to Attach files with sending mail in android application?

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?

Upvotes: 4

Views: 18017

Answers (4)

user2662974
user2662974

Reputation: 31

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"));

Upvotes: 3

abhi_novice
abhi_novice

Reputation: 51

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"));

Upvotes: 0

Naveen R
Naveen R

Reputation: 55

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);

Upvotes: 0

MKJParekh
MKJParekh

Reputation: 34301

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.

Upvotes: 5

Related Questions