【问题标题】:Attachments can not be sent out through email无法通过电子邮件发送附件
【发布时间】:2015-07-23 03:22:19
【问题描述】:

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

【问题讨论】:

  • 您当前的数据库路径错误,应该是 Environment.getExternalStorageDirectory()+"/Android/data/com.dw.inspe....." 等

标签: android android-intent email-attachments email


【解决方案1】:
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);

【讨论】:

    【解决方案2】:
    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);
            }
        }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 2013-02-21
      • 2019-10-12
      • 2021-04-10
      • 2015-09-30
      • 2012-01-26
      • 2014-05-28
      • 1970-01-01
      相关资源
      最近更新 更多