Pesquisar neste blogue

terça-feira, 13 de novembro de 2018

JAVAMAIL: Cleaning temporary files used as attachments

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
private static void cleanAttachments(MimeMessage message) {
   try {
 if (message.getContent() != null) {
  MimeMultipart multi = (MimeMultipart) message.getContent();
  for (int j = 0; j < multi.getCount(); j++) {
   BodyPart bodyPart = multi.getBodyPart(j);
   if (bodyPart.getDataHandler().getDataSource()
     instanceof FileDataSource) {
    FileDataSource f = (FileDataSource)
    bodyPart.getDataHandler().getDataSource();
    f.getFile().delete();
   }
  }
 }
  } catch (MessagingException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
}

For when you use File.createTempFile  to support the attachments of a mail message and cannot use deleteOnExit because the JVM will not exit.
Something i saw myself needing.

All the best.

Sem comentários:

Enviar um comentário