如何只使用電子郵件應用程序發送帶有附件的電子郵件?

[英]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.

兩種方法都有小缺點,我給你們展示兩種方法現在由你們來選擇。


注意!

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



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