附件不能通过电子邮件发送出去

时间:2015-05-12 10:08:42

标签: android android-intent email-attachments email-integration

我正在尝试从我的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);
}

2 个答案:

答案 0 :(得分:0)

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);
        }
    }

我希望它会帮助你。

答案 1 :(得分:0)

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());
        }

    }
  

称之为

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