Sending Email with attachment, sends a blank file.
- by pankaj
I am using this code:
File myDir = new File(getApplicationContext().getFilesDir().getAbsolutePath());
try {
Log.i("CSV Testing ", "CSV file creating");
FileWriter fw = new FileWriter(myDir + "/myfile.csv");
//
// write data to file
//
Log.i("CSV Testing ", "CSV file created and your data has been saved");
// Process for sending email with CSV file
File CSVFile = new File(myDir,"myfile.csv");
if(CSVFile.exists()) {
Log.i("CSV FILE", "CSV file exists");
} else {
Log.i("CSV FILE", "CSV file not exists");
}
Log.i("SEND EMAIL TESTING", "Email sending");
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/csv");
emailIntent .putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"myemailid"});
emailIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, "my subject");
emailIntent .putExtra(android.content.Intent.EXTRA_TEXT, "_____________\n Regards \n Pankaj \n ____________ ");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + CSVFile.getAbsolutePath()));
emailIntent.setType("message/rfc822"); // Shows all application that supports SEND activity
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
showMSG("There are no email clients installed.");
}
Log.i("SEND EMAIL TESTING", "Email sent");
} catch (FileNotFoundException e) {
Log.i("ExportCSV Exception", e.toString());
} catch (IOException e) {
Log.i("ExportCSV Exception", e.toString());
}
But it sends myfile.csv as a blank file. I checked it from file explorer, where myfile.csv is not blank and contains right data. How can I solve this?