Search code examples
jakarta-mailpop3winmail.dat

get email attatchment for POP 3 received as winmail.dat


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
            }   
        }

Solution

  • 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.