1
votes

Just as the title states, Im creating a new file and then sending that file as an email attachment

File Creation Code:

newFile = new File("/sdcard/" + TEMPFILENAME);
if (newFile.exists()) {
    newFile.delete();
}
newFile.createNewFile();
for (int j = 0; j < arrayList.size(); j++) {
    writeFileToStorage(TEMPFILENAME, arrayList.get(j).AccountName, arrayList.get(j).UserName, encryptString(arrayList.get(j).Password));
}
recipient = emailEdit.getText().toString();
new EmailSender().execute();

then in my email service class I have the following

MimeMessage message = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file.getAbsolutePath());
DataHandler handler = new DataHandler(source);

message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

messageBodyPart.setDataHandler(handler);
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);
Transport.send(message);

The file is getting sent with the correct name and from the correct recipient/sender but the file is always empty.

Any ideas?

Edit:

Implementation of writeFileToStorage, which works in the app regularly when saving data

try {
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(openFileOutput(fName, MODE_APPEND)));
    bufferedWriter.write(aName);
    bufferedWriter.newLine();
    bufferedWriter.write(_uName);
    bufferedWriter.newLine();
    bufferedWriter.write(_pWord);
    bufferedWriter.newLine();
    bufferedWriter.close();
    Log.d("Wrote: ", ("AccountName: "+ aName + " UserName: " + _uName + " Password: " + _pWord+ " to: " + fName));
} catch (Exception err) {
    String writeError = getString(R.string.write_error);
    Toast.makeText(getBaseContext(), writeError, Toast.LENGTH_SHORT).show();
}
1
Show the implementation of: writeFileToStorageRyan J
Are you closing the file?323go
Never hardcode root paths. Always use methods to derive top-level directories in Android. Hence, please use getFilesDir(), getExternalFilesDir(), etc. Also, never use concatenation to build a file path. Use the File constructor that takes the root directory (as a File) and the name of the file you want in that directory. Also, please do not clutter up the user's external storage with your files. And, you are not passing the File into writeFileToStorage(), and so nobody knows where you are writing this data to.CommonsWare
CommonsWare, the app is a password saving app, if the user upgrades their phone there is no way to transfer the data. This is allowing them to do so. the file gets deleted after the email is sent either way, plus it's only a couple of megabytes of data. getExternalFilesDir is throwing a FileNotFoundExceptionski6154
Before you send the file by email and on the receiver side discover that you received nothing you could check if the file exists and check its file size.greenapps

1 Answers

0
votes

It would be interesting how you wrote the writeFileToStorage(...) Method.

After calling newFile.createNewFile(); your newFile won't get manipulated.

Edit: Your writeFileToStorage(...) Method doesnt seem to work. The parameter fname is not correct.