简体   繁体   中英

JavaMail unable to read XML attachments

I've developed a Java Client application which I use to download my own email. I've found out that I'm unable to find some attachments within the email, in particular the XML file that I receive as receipt when I send an email to a certified company. The code I use for downloading attachments:

private void getAttachment(Message message) throws Exception {

    Multipart multipart = (Multipart) message.getContent();

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

        if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())
                && StringUtils.isEmpty(bodyPart.getFileName())) {
            continue; // dealing with attachments only
        }
        InputStream is = bodyPart.getInputStream();
        File f = new File("tmp/" + bodyPart.getFileName());
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buf = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buf)) != -1) {
            fos.write(buf, 0, bytesRead);
        }
        fos.close();

    }

}

My question is: is there any restriction in downloading XML attachments ? or should I rather hook for different methods of the Message class ? Thanks

The "ATTACHMENT" disposition and the filename are only recommendations, not requirements. Some messages will have "attachments" without setting these. In most cases you should consider any body part after the first in a multipart/mixed message to be an attachment. See the JavaMail FAQ for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM