Suhaybovic
Suhaybovic

Reputation: 67

Send text file attachment with an email

I am trying to attach a text file in order to send it with an email but whenever i open the Email app it says that the file does not exist Help Please

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");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(directory+"/data.txt"));

try {
     startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(getBaseContext(), "An Error Happened ", Toast.LENGTH_SHORT).show();
}

Upvotes: 3

Views: 3141

Answers (1)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

If you are trying to send files with email, make sure that they are on a public accessible location and that you tell it that it is a file you want to send. Use this code as an example.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822").putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}).putExtra(android.content.Intent.EXTRA_SUBJECT, "Mail subject").putExtra(android.content.Intent.EXTRA_TEXT, "lalalala");
String targetFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tmp" + File.separator + "test.txt";
Uri attachmentUri = Uri.parse(targetFilePath);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentUri)); 

Upvotes: 4

Related Questions