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

[英]How to send Email with attachment using only email apps?


There are two requirements:

有两个要求:

  • Email with attachment
  • 电子邮件与附件
  • In the Intent chooser, there should be only Email apps.
  • 在意向选择器中,应该只有电子邮件应用程序。

What I have known/done:

我知道/做什么:

  • Intent.ACTION_SENDTO with intent.setData(Uri.parse("mailto:")) can make sure that there are only Email apps in Intent chooser but it will not bring attachment(For some apps like Gmail it will, but there are also many apps that will ignore attachment).

    意图。setdata (Uri.parse(“mailto:”))可以确保在Intent chooser中只有电子邮件应用程序,但它不会带来附件(对于一些应用程序,如Gmail,它会,但也有很多应用程序会忽略附件)。

  • Intent.ACTION_SEND can send Email with attachment. However, in Intent chooser, there will be apps that are actually not Email apps but can response to Intent.ACTION_SEND. Using intent.setType("message/rfc822") can reduce number of those apps but not all.

    意图。ACTION_SEND可以用附件发送电子邮件。然而,在Intent chooser中,有一些应用程序实际上不是电子邮件应用程序,而是可以响应Intent. action_send。使用int . settype(“message/rfc822”)可以减少这些应用的数量,但不是全部。

  • References this answer: https://stackoverflow.com/a/8550043/3952691 and nearly succeed in my goals. My code is as below:

    参考这个答案: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);
        }
    }
    

    However, on some devices(For example, Xiaomi 2S with MIUI V5, I don't know if this can be influenced by a third-party rom), the result is an empty Intent chooser. And it seems that above Android 6.0, Intent.EXTRA_INITIAL_INTENTS has some bugs(Custom intent-chooser - why on Android 6 does it show empty cells?, and another one: https://code.google.com/p/android/issues/detail?id=202693).

    然而,在某些设备上(例如MIUI V5上的小米2S,我不知道这是否会受到第三方rom的影响),结果是一个空的意图选择器。在Android 6.0的上面,似乎有意图。EXTRA_INITIAL_INTENTS有一些bug(自定义意图选择器——为什么在Android 6上显示空单元格?)还有一个:https://code.google.com/p/android/issues/detail?id=202693)。

As a result, I don't know how to achieve my goals. Please help me, thank you in advance.

结果,我不知道如何实现我的目标。请帮助我,提前谢谢你。

3 个解决方案

#1


0  

Try the below code to Send a mail

尝试以下代码发送邮件

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..."));

#2


-1  

Try this more efficient:

试试这个更有效:

  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..."));

#3


-2  

There are two ways to do this

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

OPTION 1

选项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, ""));

Result

结果

enter image description here

OPTION 2

选项2

It works perfactly except it wont filter out FTP

它能有效地工作,除非它不能过滤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, ""));

Result

结果

enter image description here

Both ways have minor flaws I show you both ways it is now upto you to pick one.

两种方法都有小缺点,我给你们展示两种方法现在由你们来选择。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/05/12/72029251969dc6d9fbf96a3048ac1976.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告