Using Android Intent.ACTION_SEND for sending email

前端 未结 17 1196
野趣味
野趣味 2020-11-29 23:07

I\'m using Intent.ACTION_SEND to send an email. However, when I call the intent it is showing choices to send a message, send an email, and also to

相关标签:
17条回答
  • 2020-11-29 23:43

    [Solution for API LEVEL >=15]

    I've finally succeded in sending email WITH attachments to ONLY email clients. I write it here because it took me a lot of time and it may be usefull to others.

    The problem is:

    • Intent.ACTION_SENDTO takes Data URI (so you can specify "mailto:" schema) BUT it does not accept Intent:EXTRA_STREAM.

    • Intent.ACTION_SEND accepts Intent:EXTRA_STREAM (so you can add attachment) BUT it takes only Type (not Data URI so you cannot specify "mailto:" schema).

    So Intent.ACTION_SEND lets the user choose from several Activities, even if you setType("message/rfc822"), because that App/Activities can manage all file types (tipically GDrive/Dropbox Apps) and so even email message files.

    The solution is in the setSelector method. With this method you can use Intent.ACTION_SENDTO to select the Activity, but then send the Intent.ACTION_SEND Intent.

    Here my solution code (the attachment came from a FileProvider, but it could be any file):

    {
        Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO);
        emailSelectorIntent.setData(Uri.parse("mailto:"));
    
        final Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"address@mail.com"});
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        emailIntent.setSelector( emailSelectorIntent );
    
        Uri attachment = FileProvider.getUriForFile(this, "my_fileprovider", myFile);
        emailIntent.putExtra(Intent.EXTRA_STREAM, attachment);
    
        if( emailIntent.resolveActivity(getPackageManager()) != null )
            startActivity(emailIntent);
    }
    
    0 讨论(0)
  • 2020-11-29 23:44

    Shout-out to ARLabs for posting the best solution on how to send an email on android. I just made a little update - an option to add multiple email attachements.

    fun sendEmail(context: Context, recipients: List<String>?, subject: String?, body: String?, attachments: List<Uri>?) {
        val emailIntent = Intent(Intent.ACTION_SEND_MULTIPLE)
        emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
        emailIntent.selector = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
    
        if (recipients != null) {
            val recipientsArray = arrayOfNulls<String>(recipients.size)
            ArrayList(recipients).toArray(recipientsArray)
            emailIntent.putExtra(Intent.EXTRA_EMAIL, recipientsArray)
        }
        if (subject != null) {
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
        }
        if (body != null) {
            emailIntent.putExtra(Intent.EXTRA_TEXT, body)
        }
        if (attachments?.isNotEmpty() == true) {
            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(attachments))
        }
    
        if (emailIntent.resolveActivity(context.packageManager) != null) {
            context.startActivity(emailIntent)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 23:46

    Best code to restrict it to only send an email. set this type to only send an email. i.setType("message/rfc822");

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"skapis1@outlook.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
            i.putExtra(Intent.EXTRA_TEXT   , "body of email");
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(Firstclass.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
    
    0 讨论(0)
  • 2020-11-29 23:48

    This will help you.

    On your button click : 
    
    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[]{"youremail@yahoo.com"});          
    email.putExtra(Intent.EXTRA_SUBJECT, "subject");
    email.putExtra(Intent.EXTRA_TEXT, "message");
    email.setType("message/rfc822");
    startActivity(Intent.createChooser(email, "Choose an Email client :"));
    
    0 讨论(0)
  • 2020-11-29 23:50

    This will open only the Email app installed in your android phone.

            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:"));
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"});
            intent.putExtra(Intent.EXTRA_SUBJECT, "email subject");
            intent.putExtra(Intent.EXTRA_TEXT, "message body");
    
            try {
                startActivity(Intent.createChooser(intent, "send mail"));
            } catch (ActivityNotFoundException ex) {
                Toast.makeText(this, "No mail app found!!!", Toast.LENGTH_SHORT);
            } catch (Exception ex) {
                Toast.makeText(this, "Unexpected Error!!!", Toast.LENGTH_SHORT);
            }
    
    0 讨论(0)
  • 2020-11-29 23:50
    public class MainActivity extends AppCompatActivity {
    
      private EditText edt_email;
      private Button btn_send;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        edt_email = (EditText) findViewById(R.id.edt_email);
        btn_send = (Button) findViewById(R.id.btn_send);
    
        btn_send.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
    
            Intent intent = new Intent(Intent.ACTION_SEND );
            intent.putExtra(Intent.EXTRA_EMAIL , new String[]{"sanaebadi97@gmail.com"});
            intent.putExtra(Intent.EXTRA_SUBJECT , "subject");
            intent.putExtra(Intent.EXTRA_TEXT , "My Email Content");
            intent.setType("message/rfc822");
            startActivity(Intent.createChooser(intent , "Choose Your Account : "));
          }
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题