views:

662

answers:

4

Hi All :
I have to send mail along with embedded image. Once the mail has been sent, image in the application server should be deleted immediately. Problem i am facing is, after mail sent the control goes method which contain

      File file = new File("../bar.jpeg")
      if(file.exists()){
     file.delete();
     System.out.println("Barcode Image Deleted");
      }

Its printing "Barcode Image Deleted". But image in the folder is not getting deleted and it still present in same location. I am using multipart to attach image in mail. Can anyone help me why its not getting deleted?

+3  A: 

File.delete() returns a true/false condition. Try checking the return condition of delete to see if the system is actually reporting the file as deleted.

tschaible
It wont delete the image file which is been embedded in mail?
Hi.. I have checked by putting it in if/else block.. It return true only and image not get deleted....
+6  A: 

The File.delete method returns a boolean which indicates whether the deletion was successful.

It could be that the file deletion is not being successfully performed due to not having permissions to delete the file.

coobird
+3  A: 

Firstly, File.delete() returns a boolean if it successfully deletes a file. Check that value and at least log it.

If it is not being deleted, then I would guess that either

  1. the file is currently open for reading and the OS won't let you delete it until it is closed. Possible your mail software? My guess is that the mail software does not try to base64 encode the image (for inclusion in the message) until it is actually sending the message...and/or does not stop reading it until the mail is sent.
  2. the java process does not have permissions to delete the file
Stu Thompson
1. It wont delete the image file which is been embedded in mail? 2. I can delete the image before and now only its not deleting!!!
If your Java code cannot delete the image before the mail is even created, the I could guess a 'permissions' issues. If your Java code tries to delete the image *immediately* after sending the mail (with image in it), then I would guess that the mail software is still using the image. How are you sending the mail?
Stu Thompson
Yeah. I have given file delete code right after Transport.send(msg); Shouldn't i???
That is your problem, then! You need to wait until the message has actually been sent to the SMPT server.
Stu Thompson
How do make my controller to wait till that?. One more thing, i have given "file.delete()" in if condition and it goes to true block. i dont know why!!!!
As @spdenne says, you'll want to use register a TransportListener. Once the message is actually sent, it will trigger your code, and then you can delete the file.
Stu Thompson
+7  A: 

Are you using javax.mail?
If so, you'll need to wait till the mail has finished being sent, which you find out about by registering a TransportListener.

This also means you won't be able to use the static Transport.send() methods, but will have to construct and clean up your own session and transport.

I'm trying to remember details from a while ago... I think the DataHandler or DataSource don't close the input stream when they've finished reading it, so you need to keep a reference to it and close it yourself before you can delete the underlying file.

Stephen Denne
Yeah. I have given file delete code right after Transport.send(msg);Shouldn't i???
Not necessarily, because Transport.send() may not be synchronous. It may teake some time after send() is called before something happens in the background, which is why you need the TransportListener callback.
skaffman