0

I am trying to send a mail with an attachment. I am using javamail api to perform this operation. Since multiple users can send mail at the same time, I created a thread to make it safe. I am able to delete the file using the file.delete() function which happening before the attachment is done in mail. But I am unable to delete the file after attachment/mail sent. Please help me in this issue.

Here is the code I have used to attach and send mail:

public void sendMailWithAttachment(String from, final String to, final String subject, final String msg, final String filePath) {
    Thread ty = new Thread(){
        public void run(){
            MimeMessage message = mailSend.createMimeMessage();

             try{
                MimeMessageHelper helper = new MimeMessageHelper(message, true);

                //helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(msg);

                FileSystemResource file = new FileSystemResource(filePath);
                helper.addAttachment(file.getFilename(), file);

         //I want to write code to delete the file here

             }catch (MessagingException e) {
                 throw new MailParseException(e);
             }
             mailSend.send(message);
        }
    };
    ty.start();
 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
surendarmx
  • 125
  • 1
  • 2
  • 7
  • Can you elaborate on "But I am unable to delete the file"? – D3xter Apr 14 '15 at 11:18
  • Why don't you try callback on after thread operations finished. You'll easily find solution from concurrency framework introduced from java 5 onwards. May be this link gives you some clue on this: http://stackoverflow.com/questions/17255498/implementing-callbacks-in-java-with-runnable – Pankaj Dubey Apr 14 '15 at 11:19
  • @AndreasSinz Sorry, my mistake, I am unable to delete the file, but its getting deleted before the attachment is done. – surendarmx Apr 14 '15 at 11:47
  • @PankajDubey Thanks for the tutorial. But I am not sure about callback here, is that the where i can get to know the thread gets finished or something like that? – surendarmx Apr 14 '15 at 12:10
  • @surendarmx yeah! you got it. In an asynchronous operation we are not sure how long will it take to complete that particular task so we use callbacks so that whenever that task completes automatically it fires and does the tasks like this (file deletion, memory clearing etc). Hope this will help. – Pankaj Dubey Apr 14 '15 at 12:12

3 Answers3

1

Anyway you're initializing your Thread implementation. You can override the finalize() method for the Thread implementation which gets called when the Thread object is about to get garbage collected.

Code would look like below:

Thread ty = new Thread(){
    public void run(){
            // do mail sending stuff here.
    }

    @Override
    protected void finalize() throws Throwable {
        // delete file here
    }
};
ty.start();
Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
0

You have various ways to do this task, I recommend you the second

1) Instiatate that File, And then if your java environment has the corrects permissions on that folder you can delete that file, like this

try{

            File file = new File("filePath");

            if(file.delete()){
                System.out.println("Deleted file: " + file.getName());
            }else{
                System.out.println("Delete failed on file ": + file.getName());
            }

        }catch(Exception e){

            e.printStackTrace();

        }

2) You can also use The generic Class Files

try {
    Files.delete(path);
} catch (NoSuchFileException x) {
    System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
    System.err.format("%s not empty%n", path);
} catch (IOException x) {
    // File permission problems are caught here.
    System.err.println(x);
}

If you have problems deleting the file maybe is beacuse the object FileSystemResource is pointing that file, Try to finalize that object with

file.finalize()

take a look at this question to finished Threads

How to know if other threads have finished?

Community
  • 1
  • 1
anquegi
  • 11,125
  • 4
  • 51
  • 67
  • This is a function to delete the file. I am using the first option to delete the file, but my problem is before attachment is done in mail, the file getting deleted. Please help me in that . Thanks – surendarmx Apr 14 '15 at 11:48
  • If you have problems deleting the file maybe is beacuse the object FileSystemResource is pointing that file, Try to finalize that object with file.finalize() – anquegi Apr 14 '15 at 11:54
  • Thanks but no luck.. Any suggestion how to know the thread has finished ? – surendarmx Apr 14 '15 at 12:09
  • I added this question http://stackoverflow.com/questions/702415/how-to-know-if-other-threads-have-finished – anquegi Apr 14 '15 at 12:20
0

Delete the file in a finally block after the send operation.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40