无法通过Gmail以编程方式发送带附件的电子邮件

时间:2017-04-21 11:31:47

标签: android gmail android-support-library

我正在开发一个Android应用程序,该应用程序组成带有照片附件的电子邮件,并通过第三方应用程序发送。

要以编程方式撰写电子邮件,请使用IntentBuilder类。

/**
 * Helpers to compose and send emails with 3rd party apps.
 */
public final class MailUtils {

    /**
     * Creates {@link Intent} and fills data to send email with 3rd party application.
     *
     * @param activity
     * @param photoUris list of photo URIs which can be accessed with {@link android.support.v4.content.FileProvider}
     * @param requestCode to filter results in {@link Activity#onActivityResult(int, int, Intent)}
     */
    public static void sendMail(Activity activity, List<Uri> photoUris, int requestCode) {
        final ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(activity)
                .setType("text/html")
                .setChooserTitle("Choose application to send email")
                .addEmailTo("someone@anywhere.com")
                .addEmailCc("someone.else@anywhere.com")
                .setSubject("This is subject")
                .setHtmlText("Hello, world!");
        if (photoUris != null) {
            for (Uri photoUri : photoUris) {
                intentBuilder.addStream(photoUri); // add photo attachment
            }
        }
        if (activity.getPackageManager().resolveActivity(intentBuilder.getIntent(), 0) != null) {
            final Intent intent = intentBuilder.createChooserIntent();
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            activity.startActivityForResult(intent, requestCode);
        } else {
            Toast.makeText(activity, "Email app not found", Toast.LENGTH_LONG).show();
        }
    }
}

错误通知

Couln't send attachment error

Gmail仅发送电子邮件文本,但会跳过附件。此问题仅在 Gmail Android应用中存在。一切都适用于其他电子邮件应用(例如 Outlook Yahoo )。

注意:我的情况下照片附件的大小通常小于10MB。

对于解决方法,我尝试在单个ZIP存档中压缩照片,但结果几乎相同。它适用于5-10张压缩照片,但可以拍摄超过10张照片。

在设备上测试

  • LG Nexus 4(Jelly Bean 4.2.2,Gmail 6.11.27...
  • LG Nexus 5X(牛轧糖7.1.2,Gmail 7.3.26...
  • 三星Galaxy Note 4(Marshmallow 6.0.1,Gmail 7.3.26...

2 个答案:

答案 0 :(得分:0)

我认为OP在评论中提到的答案是使用FileProvider临时授予对要附加的文件的访问权限。详情请见:https://developer.android.com/training/secure-file-sharing/setup-sharing.html

我还发现本教程很有用:https://drivy.engineering/android-fileprovider/

答案 1 :(得分:-2)

当您附加文件时,需要一些时间来附加邮件,因此您必须使用AsyncTask执行操作。请尝试使用此代码与Gmail配合使用,并使用7.0操作系统。

private class attachMailProcess extends AsyncTask<Void, Void, Void>
{
    protected void onPreExecute()
    {
        // Progress visibility Visible
    }

    protected Void doInBackground(Void... param)
    {
        YourActivity.this.runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                File file = // Get your file here.
                if (file.exists()) {
                    Uri path = Uri.fromFile(file);
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("message/rfc822");
                    i.putExtra(Intent.EXTRA_EMAIL, new String[]{"Your email"});
                    i.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
                    i.putExtra(Intent.EXTRA_TEXT, "Your body text");
                    i.putExtra(Intent.EXTRA_STREAM, path);
                    try {
                        startActivity(Intent.createChooser(i,"Email Chooser"));
                        // Progress visibility gone
                    } catch (android.content.ActivityNotFoundException ex) {
                        // Progress visibility gone
                        Toast.makeText(NewRoundActivity.this, "No Email client found", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    //File not found
                }
            }
        });
        return null;
    }

    protected void onPostExecute(Void result) {
        // Progress visibility gone
    }
}