Send Email Intent

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

    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 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 getAttachmentUriList()
    {
            ArrayList 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 appList = context.getPackageManager().queryIntentActivities(queryIntent, PackageManager.MATCH_DEFAULT_ONLY);
    
            Intent finalIntent = null;
    
            if (!appList.isEmpty())
            {
                List targetedIntents = new ArrayList();
    
                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;
    }
    

提交回复
热议问题