Sending email with attachment in Android programmatically

I use the following code to send email from my android application:

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"email@yahoo.com"});          
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "message");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
email.setType("plain/text");
startActivity(Intent.createChooser(email, "Choose an Email App:"));

This works great for all email sending applications, but shows too many options like Facebook, Twitter, Bluetooth to send this email. I just wanted to see email applications to choose from.

So, I replaced email.setType("plain/text");with email.setType ("message / rfc822");

Now it shows only mail applications and works fine for all mail applications installed on my device except Outlook. Outlook does not send the attachment properly. In the end, I get weird characters instead of an attached file.

Then I replaced email.setType("message/rfc822");with email.setType ("application / octet-stream");

This solved the problem with Outlook attachments, but now I can not send emails using the default Android email application. It sends email without attachments.

+4
source share
3 answers

Use this code to attach a file and send an email

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, "subject");
email.putExtra(Intent.EXTRA_TEXT, "text");
Uri uri = Uri.parse("file://" + myFile.getAbsolutePath());
email.putExtra(Intent.EXTRA_STREAM, uri);
email.setType("message/rfc822");
startActivity(email);
+1
source

Use Intent.ACTION_SENDTO instead of Intent.ACTION_SEND.

0
source

You just want to filter out applications that can send emails, and want to show them all.

At your request, I have found the best answer. Take a look at this answer.

I think this is what you want for sure .

0
source

All Articles