Send Email Intent

前端 未结 30 3289
忘掉有多难
忘掉有多难 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:00

    There are three main approaches:

    String email = /* Your email address here */
    String subject = /* Your subject here */
    String body = /* Your body here */
    String chooserTitle = /* Your chooser title here */
    

    1. Custom Uri:

    Uri uri = Uri.parse("mailto:" + email)
        .buildUpon()
        .appendQueryParameter("subject", subject)
        .appendQueryParameter("body", body)
        .build();
    
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
    startActivity(Intent.createChooser(emailIntent, chooserTitle));
    

    2. Using Intent extras:

    Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, body);
    //emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text
    
    startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
    

    3. Support Library ShareCompat:

    Activity activity = /* Your activity here */
    
    ShareCompat.IntentBuilder.from(activity)
        .setType("message/rfc822")
        .addEmailTo(email)
        .setSubject(subject)
        .setText(body)
        //.setHtmlText(body) //If you are using HTML in your body text
        .setChooserTitle(chooserTitle)
        .startChooser();
    
    0 讨论(0)
  • 2020-11-22 08:00

    Try:

    intent.setType("message/rfc822");
    
    0 讨论(0)
  • 2020-11-22 08:00

    SEND TO EMAIL CLIENTS ONLY - WITH MULTIPLE ATTACHMENTS

    There are many solutions but all work partially.

    mailto properly filters email apps but it has the inability of not sending streams/files.

    message/rfc822 opens up hell of apps along with email clients

    so, the solution for this is to use both.

    1. First resolve intent activities using mailto intent
    2. Then set the data to each activity resolved to send the required data
    private void share()
    {
         Intent queryIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
         Intent dataIntent  = getDataIntent();
    
         Intent targetIntent = getSelectiveIntentChooser(context, queryIntent, dataIntent);
         startActivityForResult(targetIntent);
    }
    

    Build the required data intent which is filled with required data to share

    private Intent getDataIntent()
    {
            Intent dataIntent = buildIntent(Intent.ACTION_SEND, null, "message/rfc822", null);
    
            // Set subject
            dataIntent.putExtra(Intent.EXTRA_SUBJECT, title);
    
            //Set receipient list.
            dataIntent.putExtra(Intent.EXTRA_EMAIL, toRecipients);
            dataIntent.putExtra(Intent.EXTRA_CC, ccRecipients);
            dataIntent.putExtra(Intent.EXTRA_BCC, bccRecipients);
            if (hasAttachments())
            {
                ArrayList<Uri> uris = getAttachmentUriList();
    
                if (uris.size() > 1)
                {
                    intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                    dataIntent.putExtra(Intent.EXTRA_STREAM, uris);
                }
                else
                {
                    dataIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris.get(0));
                }
            }
    
            return dataIntent;
    }
    
    protected ArrayList<Uri> getAttachmentUriList()
    {
            ArrayList<Uri> uris = new ArrayList();
            for (AttachmentInfo eachAttachment : attachments)
            {
                uris.add(eachAttachment.uri);
            }
    
            return uris;
    }
    

    Utitlity class for filtering required intents based on query intent

    // Placed in IntentUtil.java
    public static Intent getSelectiveIntentChooser(Context context, Intent queryIntent, Intent dataIntent)
    {
            List<ResolveInfo> appList = context.getPackageManager().queryIntentActivities(queryIntent, PackageManager.MATCH_DEFAULT_ONLY);
    
            Intent finalIntent = null;
    
            if (!appList.isEmpty())
            {
                List<android.content.Intent> targetedIntents = new ArrayList<android.content.Intent>();
    
                for (ResolveInfo resolveInfo : appList)
                {
                    String packageName = resolveInfo.activityInfo != null ? resolveInfo.activityInfo.packageName : null;
    
                    Intent allowedIntent = new Intent(dataIntent);
                    allowedIntent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
                    allowedIntent.setPackage(packageName);
    
                    targetedIntents.add(allowedIntent);
                }
    
                if (!targetedIntents.isEmpty())
                {
                    //Share Intent
                    Intent startIntent = targetedIntents.remove(0);
    
                    Intent chooserIntent = android.content.Intent.createChooser(startIntent, "");
                    chooserIntent.putExtra(android.content.Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[]{}));
                    chooserIntent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
                    finalIntent = chooserIntent;
                }
    
            }
    
            if (finalIntent == null) //As a fallback, we are using the sent data intent
            {
                finalIntent = dataIntent;
            }
    
            return finalIntent;
    }
    
    0 讨论(0)
  • 2020-11-22 08:01

    Please use the below code :

                    try {
    
                        String uriText =
                                "mailto:emailid" +
                                        "?subject=" + Uri.encode("Feedback for app") +
                                        "&body=" + Uri.encode(deviceInfo);
                        Uri uri = Uri.parse(uriText);
                        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
                        emailIntent.setData(uri);
                        startActivity(Intent.createChooser(emailIntent, "Send email using..."));
                    } catch (android.content.ActivityNotFoundException ex) {
                        Toast.makeText(ContactUsActivity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
                    }
    
    0 讨论(0)
  • 2020-11-22 08:02

    Using intent.setType("message/rfc822"); does work but it shows extra apps that not necessarily handling emails (e.g. GDrive). Using Intent.ACTION_SENDTO with setType("text/plain") is the best but you have to add setData(Uri.parse("mailto:")) to get the best results (only email apps). The full code is as follows:

    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setType("text/plain");
    intent.setData(Uri.parse("mailto:IT@RMAsoft.NET"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "Email from My app");
    intent.putExtra(Intent.EXTRA_TEXT, "Place your email message here ...");
    startActivity(Intent.createChooser(intent, "Send Email"));
    
    0 讨论(0)
  • 2020-11-22 08:03

    when you will change your intent.setType like below you will get

    intent.setType("text/plain");
    

    Use android.content.Intent.ACTION_SENDTO to get only the list of e-mail clients, with no facebook or other apps. Just the email clients. Ex:

    new Intent(Intent.ACTION_SENDTO);
    

    I wouldn't suggest you get directly to the email app. Let the user choose his favorite email app. Don't constrain him.

    If you use ACTION_SENDTO, putExtra does not work to add subject and text to the intent. Use Uri to add the subject and body text.

    EDIT: We can use message/rfc822 instead of "text/plain" as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.

    message/rfc822 supports MIME Types of .mhtml, .mht, .mime

    0 讨论(0)
提交回复
热议问题