简体   繁体   中英

ANDROID | send email attachment only with email clients

Please read the my question carefully before marking it duplicate. thanks :)

I want to send an email with attachment. I have used ACTION_SENDTO

Code:

 Intent intent = new Intent(Intent.ACTION_SENDTO);
 intent.setData(Uri.parse("mailto:"));
 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{
          Wrapper.INSTANCE.getSupportEmail()});
 intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(resourceTitleId));
 intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));

 try {
        ctx.startActivity(Intent.createChooser(intent, "Send mail..."));
 } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(ctx, "There are no email clients installed.",
                        Toast.LENGTH_SHORT).show();
 }

It shows only email clients, see the picture below:

测试

but attachment is not working except GMAIL.

I have also tried to achieve it with ACTION_SEND

CODE:

 Intent intent = new Intent(Intent.ACTION_SEND);
 intent.putExtra(Intent.EXTRA_EMAIL, new String[]{
                    Wrapper.INSTANCE.getSupportEmail()});
 intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(resourceTitleId));
 intent.setType("message/rfc822");
 intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath));

it shows all the supported apps, please see the picture below:

在此处输入图片说明

but attachement is working file on all email clients.

I have read on many stack-overflow answers that

intent.setType("message/rfc822");

this will solve my problem. but it does not.

I want only email clients to be shown that also supports attachment.

String recepientEmail = ""; // either set to destination email or leave empty

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
startActivity(intent);

The point is to use ACTION_SENDTO as action and mailto: as data. If you want to let the user specify the destination email, use just mailto:; if you specify email yourself, use mailto:name@domain.com

Suggested method filters all the application, that can send email(such as default email app or gmail)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM