0

Using the code below, I can attach files to an email from my application with no problem - if I use the Gmail application as my email client. However, any other email client disregards the attachment I send to it.

Here is my code:

public static void sendEmail(Context context, String toAddress, String subject, String body, String attachmentPath) throws Exception{
        try {
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", toAddress, null));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_SUBJECT, subject);
            intent.putExtra(Intent.EXTRA_TEXT, body);

            File file = new File(attachmentPath);
            Uri uri = Uri.fromFile(file);

            intent.putExtra(Intent.EXTRA_STREAM, uri);

            context.startActivity(intent);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
}

Does anyone know how to set up an Intent in such a way that any non-Gmail email client will recognize and accept an attachment?

Thank you.

mahdaeng
  • 791
  • 4
  • 15
  • 25

3 Answers3

3

Here's some code that works. Why it should make any difference, I don't know, but it works.

public static void sendEmail(Context context, String toAddress, String subject, String body, String attachmentPath, String attachmentMimeType) throws Exception{
        try {
            Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{toAddress});
                intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                intent.putExtra(Intent.EXTRA_TEXT, body);

                File file = new File(attachmentPath);
                Uri uri = Uri.fromFile(file);

                intent.putExtra(Intent.EXTRA_STREAM, uri);
                intent.setType(attachmentMimeType);

                context.startActivity(Intent.createChooser(intent, "Choose an email application..."));
        }
        catch(Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
}

Notice the use of ACTION_SEND instead of ACTION_SENDTO, as well as the call to setType(...). It seems to work, but it doesn't filter out the list of non-email apps that are presented as options to send to. Good enough for me for now - unless someone has any ideas on how to make this work and still filter down the list of target apps too.

Thanks to those who offered suggestions. Hopefully this will help someone else, too.

mahdaeng
  • 791
  • 4
  • 15
  • 25
  • have you seen that one: http://stackoverflow.com/questions/3132889/action-sendto-for-sending-an-email? It's said "... *(discard bluetooth and others)*..." – Trinimon Mar 22 '13 at 07:22
  • 1
    Your Sir, are my lifesaver. I nearly skipped the attempt of your code, because i thought that can't work (maybe because of low up votes) but it does work! amazing – glace May 12 '16 at 08:40
1

Do you have a specific email client in mind? A number of them don't even handle attachments in an intent or expect the intent to be populated differently.

DDRBoxman
  • 311
  • 2
  • 10
  • I guess the specific client I have in mind is the default one that comes with the Droid Razr. However, if I just target that one, then any other one might get left out, as per what Andre mentioned above. – mahdaeng Mar 21 '13 at 21:03
1

Generally this is only possible if the app vendor provides a specification about parameters you can pass in or you look into the source code. You need first to find out which activity/intent to use for the email client and afterwards, which extra's are processed in that activity.

If you are lucky you might find a list like this http://developer.android.com/guide/appendix/g-app-intents.html or that one Where is a list of available intents in Android? or the author of the app helps you to implement the right intent invocation.

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • 1
    Man! That's unfortunate. One would think that vendors would want to adhere to a standard. Crazy thinking, I know. :^) – mahdaeng Mar 21 '13 at 21:01
  • If you have a particular client in focus - just try it - may be they stick to the Google standard; anyhow, I would simply send an email to the vendor(s) 'cause they might be interested in providing such details; in the end it means their apps gets used more often :) – Trinimon Mar 21 '13 at 21:07