无法通过电子邮件发送附件

Attachments can not be sent out through email

我正在尝试从我的 android 应用发送一封带有附件的电子邮件。但是即使文件存在,电子邮件也会以某种方式在没有附件的情况下发送出去。在邮件发送视图中,显示附件(文件大小为偶数),但发送后,在接收方,没有附件。知道为什么吗?

private void copyFileToExternal() throws IOException {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "data/com.dw.inspectionhelperNSTC/databases/Inspection.db";
            String backupDBPath = "Inspection_backup.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB)
                        .getChannel();
                FileChannel dst = new FileOutputStream(backupDB)
                        .getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getBaseContext(), "testing", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getBaseContext(), "not exist", Toast.LENGTH_LONG).show();
                System.out.println("currentDB does not exists");
            }
        } else {
            Toast.makeText(MainActivity.this,
                    "NO SDcard so unable to send the database backup",
                    Toast.LENGTH_SHORT).show();
            System.out.println("sdcard cant write");
        }
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show();
        System.out.println("exception" + e.getLocalizedMessage());
    }
}

private void sendEmail(String email) {
    ArrayList<Uri> uris = new ArrayList<Uri>();
    // Adding the inspection DB;
    File file = new File(Environment.getExternalStorageDirectory(),
            "Inspection_backup.db");
    Uri path = Uri.fromFile(file);
    uris.add(path);
    // Adding the stacttrack file with uncaught expection
    File file2 = new File(Environment.getExternalStorageDirectory(),
            Constant.STACKTRACE_FILE);
    Uri path2 = Uri.fromFile(file2);
    uris.add(path2);

    Intent intent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    intent.setType("application/octet-stream");
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            "inspection database");
    String to[] = { email };
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_TEXT,
            "sending inspection database for reporting");
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    startActivityForResult(Intent.createChooser(intent, "Send mail..."),
            1222);
}
private void sendEmail(String filename)
    {

        String filePath=Environment.getExternalStorageDirectory().toString()+ File.separator+ folderName +File.separator+ filename;
        File file = new File(filePath);
        Intent i = new Intent(Intent.ACTION_SEND);
        i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        i.setType("*/*");
        i.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
        i.putExtra(Intent.EXTRA_TEXT   , "Body");
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(getApplicationContext(), "No Email Found", 5000);
        }
    }

希望对你有所帮助

public void sendMailWithIntent(String emailTo,
                                   String subject, String emailText, List<String> filePaths) {
        try {
            //need to "send multiple" to get more than one attachment
            final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                    Util.extractEmails(emailTo));
//            emailIntent.putExtra(android.content.Intent.EXTRA_CC,
//                    new String[]{emailCC});
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
            emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
            ArrayList<Uri> uris = new ArrayList<Uri>();
            //has to be an ArrayList
            if (filePaths != null) {
                //convert from paths to Android friendly Parcelable Uri's
                for (String file : filePaths) {
                    File fileIn = new File(file);
                    Uri u = Uri.fromFile(fileIn);
                    uris.add(u);
                }
            }
            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            Intent chooser = Intent.createChooser(emailIntent, "Send mail...");
            activity.startActivityForResult(chooser, 1);
        } catch (Exception e) {
            new ShowToast(context, e.getMessage());
        }

    }

Call it like

List<String> list = new ArrayList<>();
        list.add(TO_ATTACH_ONE);
        list.add(TO_ATTACH_TWO);
        sendMailTest.sendMailWithIntent(toAddresses, subject, body, list);