1

When I try to get attatchment from POP 3 mail, I am getting them as winmail.dat, not the original attached file name. How can I get the original file name?

for (int i = 0; i < multipart.getCount(); i++) 
        {
            BodyPart bodyPart = multipart.getBodyPart(i);

            if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) 
            {
                //do something
            }
            else
            {
                bodyPart.getFileName(); // here only get the winmail.dat
            }   
        }
BastianW
  • 2,628
  • 7
  • 29
  • 38
kitokid
  • 3,009
  • 17
  • 65
  • 101

1 Answers1

2

This is part of the Exchange Settings, and sadly you going to need to extract the original contents from the WinMail.dat using JTNEF.

"The Java TNEF package is an open source code implementation of a TNEF message handler, which can be used as a command-line utility or integrated into Java-based mail applications to extract the original message content."

This is found on the JavaMail's third party tools.

As alternative and what looks simpler is POI-HMEF

Sample extraction:

public void extract(String winmailFilename, String directoryName) throws Exception {
   HMEFContentsExtractor ext = new HMEFContentsExtractor(new File(winmailFilename));

   File dir = new File(directoryName);
   File rtf = new File(dir, "message.rtf");
   if(! dir.exists()) {
       throw new FileNotFoundException("Output directory " + dir.getName() + " not found");
   }

   System.out.println("Extracting...");
   ext.extractMessageBody(rtf);
   ext.extractAttachments(dir);
   System.out.println("Extraction completed");
}

There is also a sample for printing the contents here.

Dane Balia
  • 5,271
  • 5
  • 32
  • 57
  • just one question. Does all the exchange mail services return like WinMail.dat for the attachment ? – kitokid Feb 19 '13 at 05:28
  • It does appear to be Microsoft's preferred way. It appears to be used by Outlook and Exchange 2000-2010 AFAIK. But this format can be changed on Exchange Server itself. – Dane Balia Feb 19 '13 at 05:49
  • I used JTNEF and modified according to my requirements. Thanks for the input. – kitokid Feb 19 '13 at 09:17