1

Я пытаюсь отправить электронное письмо с приложением из моего приложения для 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); 
} 
+0

Ваш текущий путь дб неправильно его следует бть Environment.getExternalStorageDirectory() + "/ Android/данные/com.dw.inspe .... . »и т. д. –

ответ

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

Я надеюсь, что это поможет вам.

+0

Он работает сейчас и очень благодарит. –

+0

Пожалуйста, отметьте его правильно спасибо :) –