Send Email Intent

前端 未结 30 3285
忘掉有多难
忘掉有多难 2020-11-22 07:27
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(\"text/html\");
intent.putExtra(Intent.EXTRA_EMAIL, \"emailaddress@emailaddress.com\");
intent.putExtr         


        
相关标签:
30条回答
  • 2020-11-22 07:46

    From Android developers docs:

    public void composeEmail(String[] addresses, String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:47

    Works on All android Versions:

    String[] TO = {"email@server.com"};
        Uri uri = Uri.parse("mailto:email@server.com")
                .buildUpon()
                .appendQueryParameter("subject", "subject")
                .appendQueryParameter("body", "body")
                .build();
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    

    updated for android 10, now using kotlin.

    fun Context.sendEmail(adress:String?,subject:String?,body:String?){
    val TO = arrayOf(adress)
    val uri = Uri.parse(adress)
        .buildUpon()
        .appendQueryParameter("subject", subject)
        .appendQueryParameter("body", body)
        .build()
    val emailIntent = Intent(Intent.ACTION_SENDTO, uri)
    emailIntent.setData(Uri.parse("mailto:$adress"));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, body);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO)
    ContextCompat.startActivity(this,Intent.createChooser(emailIntent, "Send 
    mail..."),null)
    }
    
    0 讨论(0)
  • 2020-11-22 07:49

    This works for me:

    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:"));
    intent.putExtra(Intent.EXTRA_EMAIL  , new String[] { "me@somewhere.com" });
    intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
    
    startActivity(Intent.createChooser(intent, "Email via..."));
    

    i.e. use the ACTION_SENDTO action rather than the ACTION_SEND action. I've tried it on a couple of Android 4.4 devices and it limits the chooser pop-up to only display email applications (Email, Gmail, Yahoo Mail etc) and it correctly inserts the email address and subject into the email.

    0 讨论(0)
  • 2020-11-22 07:49

    Compose an email in the phone email client:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("plain/text");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    intent.putExtra(Intent.EXTRA_TEXT, "mail body");
    startActivity(Intent.createChooser(intent, ""));
    
    0 讨论(0)
  • 2020-11-22 07:50

    Most of these answers work only for a simple case when you are not sending attachment. In my case I need sometimes to send attachment (ACTION_SEND) or two attachments (ACTION_SEND_MULTIPLE).

    So I took best approaches from this thread and combined them. It's using support library's ShareCompat.IntentBuilder but I show only apps which match the ACTION_SENDTO with "mailto:" uri. This way I get only list of email apps with attachment support:

    fun Activity.sendEmail(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null) {
        val originalIntent = createEmailShareIntent(recipients, subject, file, text, secondFile)
        val emailFilterIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))
        val originalIntentResults = packageManager.queryIntentActivities(originalIntent, 0)
        val emailFilterIntentResults = packageManager.queryIntentActivities(emailFilterIntent, 0)
        val targetedIntents = originalIntentResults
                .filter { originalResult -> emailFilterIntentResults.any { originalResult.activityInfo.packageName == it.activityInfo.packageName } }
                .map {
                    createEmailShareIntent(recipients, subject, file, text, secondFile).apply { `package` = it.activityInfo.packageName }
                }
                .toMutableList()
        val finalIntent = Intent.createChooser(targetedIntents.removeAt(0), R.string.choose_email_app.toText())
        finalIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toTypedArray())
        startActivity(finalIntent)
    }
    
    private fun Activity.createEmailShareIntent(recipients: List<String>, subject: String, file: Uri, text: String? = null, secondFile: Uri? = null): Intent {
        val builder = ShareCompat.IntentBuilder.from(this)
                .setType("message/rfc822")
                .setEmailTo(recipients.toTypedArray())
                .setStream(file)
                .setSubject(subject)
        if (secondFile != null) {
            builder.addStream(secondFile)
        }
        if (text != null) {
            builder.setText(text)
        }
        return builder.intent
    }
    
    0 讨论(0)
  • 2020-11-22 07:50

    If you want to target Gmail then you could do the following. Note that the intent is "ACTION_SENDTO" and not "ACTION_SEND" and the extra intent fields are not necessary for Gmail.

    String uriText =
        "mailto:youremail@gmail.com" + 
        "?subject=" + Uri.encode("your subject line here") + 
        "&body=" + Uri.encode("message body here");
    
    Uri uri = Uri.parse(uriText);
    
    Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
    sendIntent.setData(uri);
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
       startActivity(Intent.createChooser(sendIntent, "Send message")); 
    }
    
    0 讨论(0)
提交回复
热议问题