简体   繁体   English

如何发送带有android意图的附件的电子邮件?

[英]How can I send an email with an attached file with android's intent?

I'm trying to send an email with an attached file. 我正在尝试发送附带附件的电子邮件。 The file in internal storage, so this is my code: 内部存储中的文件,所以这是我的代码:

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName");
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"mailmailmail@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));

But I obtain always: Permission denied for file . 但我总是得到: Permission denied for file

How can I solve it?? 我该怎么解决?

I solved in this way: I copy the file to send, into external cache dir, and send it. 我用这种方式解决了:我将文件复制到外部缓存目录,然后发送。

File temporaryFile = null;
    try {
        temporaryFile = File.createTempFile(keyType.getKeyTypeString(), ".pem", context.getExternalCacheDir() );
        Utils.copy(new File(getFilesDir().getAbsolutePath()+"/"+ Utils.APP_OPERATOR_DIR, keyType.getKeyTypeString()+".pem"), temporaryFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName");
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"mailmailmail@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));

-Make sure you have added the read permissions in the manifest - 确保已在清单中添加了读取权限

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE". uses-permission android:name =“android.permission.READ_EXTERNAL_STORAGE”。

-GMail 5.0 only accept files from external storage Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent . -GMail 5.0仅接受来自外部存储的文件Gmail 5.0应用程序在收到ACTION_SEND意图时失败并显示“附件权限被拒绝”

Also you can use this library: compile'com.github.yesidlazaro:GmailBackground:1.1'. 你也可以使用这个库:compile'c​​om.github.yesidlazaro:GmailBackground:1.1'。

    String imagePath = data.getStringExtra(GOTOConstants.IntentExtras.IMAGE_PATH);

    BackgroundMail.newBuilder(ReportBugActivity.this)
            .withUsername("some_email@gmail.com")
            .withPassword("pages123")
            .withMailto("mail_to_email.bugs@gmail.com")
            .withSubject("Android Bug Report")
            .withAttachments(imagePath)
            .withBody("Android Bug Report")
            .withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
                @Override
                public void onSuccess() {
                    Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();

                    finish();
                    startActivity(getIntent());
                }
            })
            .withOnFailCallback(new BackgroundMail.OnFailCallback() {
                @Override
                public void onFail() {
                    Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
                }
            }).send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM