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