Only Email apps to resolve an Intent

后端 未结 14 1540
执念已碎
执念已碎 2020-12-03 06:30

I have a problem .. I want only email activities to resolve intent ACTION.SEND but beside email I get other apps as well (e.g TubeMate) even though I have set the mime type

相关标签:
14条回答
  • 2020-12-03 07:16
    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)

    0 讨论(0)
  • 2020-12-03 07:17

    Try this

    String subject = "Feedback";
                String bodyText = "Enter text email";
                String mailto = "mailto:bob@example.org" +
                        "?cc=" + "" +
                        "&subject=" + Uri.encode(subject) +
                        "&body=" + Uri.encode(bodyText);
    
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
                emailIntent.setData(Uri.parse(mailto));
    
                try {
                    startActivity(emailIntent);
                } catch (ActivityNotFoundException e) {
                    //TODO: Handle case where no email app is available
                }
    

    Credit: https://medium.com/@cketti/android-sending-email-using-intents-3da63662c58f

    0 讨论(0)
  • 2020-12-03 07:22

    Here's a code snippet that launches ONLY email apps.

    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:example@example.com"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "This is the subject"));
    intent.putExtra(Intent.EXTRA_TEXT, "Hi, how're you doing?"));
    
    if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivity(intent);
    }
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-03 07:27

    u can also use:

    //writes messages only to email clients
    public void setWriteEmailButton() {
        btnWriteMail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(Intent.ACTION_SENDTO);
                i.setData(Uri.parse("mailto:"));
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{mConsultantInfos.getConsultantEMail()});
                i.putExtra(Intent.EXTRA_SUBJECT, mContext.getString(R.string.txtSubjectConsultantMail));
                i.putExtra(Intent.EXTRA_TEXT   , "");
                try {
                    startActivity(Intent.createChooser(i, mContext.getString(R.string.txtWriteMailDialogTitle)));
                } catch (android.content.ActivityNotFoundException ex) {
                    UI.showShortToastMessage(mContext, R.string.msgNoMailClientsInstalled);
                }
            }
        });
    }
    

    have fun (combination of both ;))

    0 讨论(0)
  • 2020-12-03 07:30

    This is an absolutely simple and 100% working approach. Thanks to the Open source developer, cketti for sharing this concise and neat solution.

    String mailto = "mailto:bob@example.org" +
        "?cc=" + "alice@example.com" +
        "&subject=" + Uri.encode(subject) +
        "&body=" + Uri.encode(bodyText);
    
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse(mailto));
    
    try {
      startActivity(emailIntent);
    } catch (ActivityNotFoundException e) {
      //TODO: Handle case where no email app is available
    }
    

    And this is the link to his/her gist.

    0 讨论(0)
  • 2020-12-03 07:33

    This works for me

    Intent intent = new Intent("android.intent.action.SENDTO", Uri.fromParts("mailto", "yourmail@gmail.com", null));
    intent.putExtra("android.intent.extra.SUBJECT", "Enter Subject Here");
    startActivity(Intent.createChooser(intent, "Select an email client")); 
    
    0 讨论(0)
提交回复
热议问题