Send Email Intent

前端 未结 30 3287
忘掉有多难
忘掉有多难 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 08:03

    The accepted answer doesn't work on the 4.1.2. This should work on all platforms:

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                "mailto","abc@gmail.com", null));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
    startActivity(Intent.createChooser(emailIntent, "Send email..."));
    

    Update: According to marcwjj, it seems that on 4.3, we need to pass string array instead of a string for email address to make it work. We might need to add one more line:

    intent.putExtra(Intent.EXTRA_EMAIL, addresses); // String[] addresses
    

    Ref link

    0 讨论(0)
  • 2020-11-22 08:03

    Edit: Not working anymore with new versions of Gmail

    This was the only way I found at the time to get it to work with any characters.

    doreamon's answer is the correct way to go now, as it works with all characters in new versions of Gmail.

    Old answer:


    Here is mine. It seems to works on all Android versions, with subject and message body support, and full utf-8 characters support:

    public static void email(Context context, String to, String subject, String body) {
        StringBuilder builder = new StringBuilder("mailto:" + Uri.encode(to));
        if (subject != null) {
            builder.append("?subject=" + Uri.encode(Uri.encode(subject)));
            if (body != null) {
                builder.append("&body=" + Uri.encode(Uri.encode(body)));
            }
        }
        String uri = builder.toString();
        Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
        context.startActivity(intent);
    }
    
    0 讨论(0)
  • 2020-11-22 08:03

    This is what I use, and it works for me:

    //variables
    String subject = "Whatever subject you want";
    String body = "Whatever text you want to put in the body";
    String intentType = "text/html";
    String mailToParse = "mailto:";
    
    //start Intent
    Intent variableName = new Intent(Intent.ACTION_SENDTO);
    variableName.setType(intentType);
    variableName.setData(Uri.parse(mailToParse));
    variableName.putExtra(Intent.EXTRA_SUBJECT, subject);
    variableName.putExtra(Intent.EXTRA_TEXT, body);
    
    startActivity(variableName);
    

    This will also let the user choose their preferred email app. The only thing this does not allow you to do is to set the recipient's email address.

    0 讨论(0)
  • 2020-11-22 08:04

    This is the proper way to send the e-mail intent according to the Android Developer Official Documentation

    Add these lines of code to your app:

    Intent intent = new Intent(Intent.ACTION_SEND);//common intent 
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    

    Optional: Add the body and subject, like this

    intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
    intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );
    

    You already added this line in your question

    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"donotemail-stack-overflow@unitmeasure-app.xyzed"});
    

    This will be the recipient's address, meaning the user will send you (the developer) an e-mail.

    0 讨论(0)
  • 2020-11-22 08:04

    I am updating Adil's answer in Kotlin,

    val intent = Intent(Intent.ACTION_SENDTO)
    intent.data = Uri.parse("mailto:") // only email apps should handle this
    intent.putExtra(Intent.EXTRA_EMAIL, Array(1) { "test@email.com" })
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    } else {
        showSnackBar(getString(R.string.no_apps_found_to_send_mail), this)
    }
    
    0 讨论(0)
  • 2020-11-22 08:04

    Use this:

    boolean success = EmailIntentBuilder.from(activity)
            .to("support@example.org")
            .cc("developer@example.org")
            .subject("Error report")
            .body(buildErrorReport())
            .start();
    

    use build gradle :

    compile 'de.cketti.mailto:email-intent-builder:1.0.0'
    
    0 讨论(0)
提交回复
热议问题