如何仅使用电子邮件应用程序发送带附件的电子邮件?

时间:2016-05-12 09:44:00

标签: android email android-intent

有两个要求:

  • 附件电子邮件
  • Intent选择器中,应该只有电子邮件应用。

我所知道/做过的事:

  • Intent.ACTION_SENDTOintent.setData(Uri.parse("mailto:"))可以确保Intent选择器中只有电子邮件应用,但不会带来附件(对于Gmail等某些应用,它会,但有还有很多会忽略附件的应用。)

  • Intent.ACTION_SEND可以发送带附件的电子邮件。但是,在Intent选择器中,会有应用实际上不是电子邮件应用,但可以响应Intent.ACTION_SEND。使用intent.setType("message/rfc822")可以减少这些应用的数量,但不是全部。

  • 参考这个答案:https://stackoverflow.com/a/8550043/3952691并且几乎成功实现了我的目标。我的代码如下:

    private static void sendFeedbackWithAttachment(Context context, String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
    
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
        if (resolveInfos.isEmpty()) {
            Toast.makeText(context, context.getString(R.string.error_activity_not_found),
                    Toast.LENGTH_SHORT).show();
        } else {
            // ACTION_SEND may be replied by some apps that are not email apps. However,
            // ACTION_SENDTO doesn't allow us to choose attachment. As a result, we use
            // an ACTION_SENDTO intent with email data to filter email apps and then send
            // email with attachment by ACTION_SEND.
            List<LabeledIntent> intents = new ArrayList<>();
            Uri uri = getLatestLogUri();
            for (ResolveInfo info : resolveInfos) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setPackage(info.activityInfo.packageName);
                i.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { Def.Meta.FEEDBACK_EMAIL });
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
                i.putExtra(Intent.EXTRA_STREAM, uri);
                intents.add(new LabeledIntent(i, info.activityInfo.packageName,
                        info.loadLabel(context.getPackageManager()), info.icon));
            }
            Intent chooser = Intent.createChooser(intents.remove(0),
                    context.getString(R.string.send_feedback_to_developer));
            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    intents.toArray(new LabeledIntent[intents.size()]));
            context.startActivity(chooser);
        }
    }
    

    但是,在某些设备上(例如,使用MIUI V5的小米2S,我不知道这是否会受到第三方ROM的影响),结果是空Intent选择器。似乎在Android 6.0之上,Intent.EXTRA_INITIAL_INTENTS有一些错误(Custom intent-chooser - why on Android 6 does it show empty cells?,另一个错误:https://code.google.com/p/android/issues/detail?id=202693)。

因此,我不知道如何实现我的目标。请帮帮我,谢谢你。

3 个答案:

答案 0 :(得分:0)

尝试以下代码发送邮件

String filename="filename.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));

答案 1 :(得分:-1)

尝试更高效:

  String[] TO = {"test@domain.com"};
    Uri uri = Uri.parse("mailto:test@domain.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..."));

答案 2 :(得分:-2)

有两种方法可以做到这一点

选项1

 Intent emailIntent = new Intent(
            android.content.Intent.ACTION_VIEW);


     //Option 1
    Uri data = Uri.parse("mailto:?subject=" + "blah blah subject"
            + "&body=" + "blah blah body" + "&to=" + "sendme@me.com");
    emailIntent.setData(data);

    startActivity(Intent.createChooser(emailIntent, ""));

结果

enter image description here

选项2

它可以正常工作,除了它不会过滤掉FTP

    //Option 2
  emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);
    emailIntent.setType("message/rfc822");
    final String[] toRecipients = new String[] { "sendme@me.com", "", };
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toRecipients);
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "blah blah subject");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
            Html.fromHtml("blah blah body"));

    startActivity(Intent.createChooser(emailIntent, ""));

结果

enter image description here

这两种方式都有轻微的缺陷我向你展示了现在由你选择的方式。