使用Java Mail下载附件

100

现在我已经下载了所有的消息,并将它们存储到

Message[] temp;

如何获取每个消息的附件列表

List<File> attachments;

注意: 请不要使用第三方库,只使用JavaMail。

5个回答

115

没有异常处理,但是这里有:

List<File> attachments = new ArrayList<File>();
for (Message message : temp) {
    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.isBlank(bodyPart.getFileName())) {
            continue; // dealing with attachments only
        } 
        InputStream is = bodyPart.getInputStream();
        // -- EDIT -- SECURITY ISSUE --
        // do not do this in production code -- a malicious email can easily contain this filename: "../etc/passwd", or any other path: They can overwrite _ANY_ file on the system that this code has write access to!
//      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();
        attachments.add(f);
    }
}

2
但是等一下,我们不应该在保存文件之前检查(bodyPart.getDisposition() == Part.ATTACHMENT)吗?这样它就不会保存电子邮件正文了吧? - George
8
StringUtils.isBlank()比使用!StringUtils.isNotBlank更加自然易读。 - Kuchi
此答案未考虑嵌套的多部分附件(例如 Thunderbird 常用)。要能够找到嵌套的多部分附件,请参见 @mefi 的答案。 - Ruslan Stelmachenko
3
这段代码存在潜在的安全漏洞。我已经编辑了这段代码以突出这一点。 - rzwitserloot
1
当然,谢谢。这段代码片段只是为了演示如何完成,自然不是生产代码。 - David Rabinowitz

34

这个问题很久了,但也许它会对某些人有帮助。我想扩展David Rabinowitz的回答。

if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()))

不能如您所期望地返回所有附件,因为您可能会有没有定义处理方式的混合部分邮件。

   ----boundary_328630_1e15ac03-e817-4763-af99-d4b23cfdb600
Content-Type: application/octet-stream;
    name="00000000009661222736_236225959_20130731-7.txt"
Content-Transfer-Encoding: base64

那么在这种情况下,你也可以检查文件名。像这样:

if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) && StringUtils.isBlank(part.getFileName())) {...}

编辑

这里有一个使用上述条件的完整工作代码。因为每个部分都可以封装另一个部分,并且附件应该被嵌套在其中,所以递归用于遍历所有部分。

public List<InputStream> getAttachments(Message message) throws Exception {
    Object content = message.getContent();
    if (content instanceof String)
        return null;        

    if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        List<InputStream> result = new ArrayList<InputStream>();

        for (int i = 0; i < multipart.getCount(); i++) {
            result.addAll(getAttachments(multipart.getBodyPart(i)));
        }
        return result;

    }
    return null;
}

private List<InputStream> getAttachments(BodyPart part) throws Exception {
    List<InputStream> result = new ArrayList<InputStream>();
    Object content = part.getContent();
    if (content instanceof InputStream || content instanceof String) {
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {
            result.add(part.getInputStream());
            return result;
        } else {
            return new ArrayList<InputStream>();
        }
    }

    if (content instanceof Multipart) {
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                result.addAll(getAttachments(bodyPart));
            }
    }
    return result;
}

该表达式检查文件名是否为空或为“null”。这正确吗? - Keerthivasan
章鱼:是的。检查CharSequence是否不为空(“”),不为null且不仅为空格。 - mefi
嘿,你能告诉我如何将List<InputStream>转换为List<File>吗? - kumuda
kumunda:嗨,您应迭代该列表并使用org.apache.commons.io.FileUtils.copyInputStreamToFile(InputStream source, File destination),并将每个文件添加到另一个集合中。如果您使用Guava,请检查Lists.transform(...),它可以替代迭代(取决于您需要如何初始化每个File实例)。 - mefi

10

以下是针对保存附件文件代码的一些时间节省技巧:

如果使用的是javax邮件版本1.4或更高版本,您可以使用以下代码:

// SECURITY LEAK - do not do this! Do not trust the 'getFileName' input. Imagine it is: "../etc/passwd", for example.
// bodyPart.saveFile("/tmp/" + bodyPart.getFileName());

替代

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

4
显然,bodyPart 需要先转换为 MimeBodyPart,像这样:((MimeBodyPart) bodyPart).saveFile("/tmp/" + bodyPart.getFileName()); - yair

6
你可以简单地使用Apache Commons Mail API MimeMessageParser - getAttachmentList(),结合Commons IO和Commons Lang。
MimeMessageParser parser = ....
parser.parse();
for(DataSource dataSource : parser.getAttachmentList()) {

    if (StringUtils.isNotBlank(dataSource.getName())) {}

        //use apache commons IOUtils to save attachments
        IOUtils.copy(dataSource.getInputStream(), ..dataSource.getName()...)
    } else {
        //handle how you would want attachments without file names
        //ex. mails within emails have no file name
    }
}

0

返回带有附件的身体部位列表。

@Throws(Exception::class)
    fun getAttachments(message: Message): List<BodyPart>{
        val content = message.content
        if (content is String) return ArrayList<BodyPart>()
        if (content is Multipart) {
            val result: MutableList<BodyPart> = ArrayList<BodyPart>()
            for (i in 0 until content.count) {
                result.addAll(getAttachments(content.getBodyPart(i)))
            }
            return result
        }
        return ArrayList<BodyPart>()
    }

    @Throws(Exception::class)
    private fun getAttachments(part: BodyPart): List<BodyPart> {
        val result: MutableList<BodyPart> = ArrayList<BodyPart>()

        if (Part.ATTACHMENT == part.disposition && !part.fileName.isNullOrBlank()){
            result.add(part)
        }

        val content = part.content
        if (content is Multipart) {
            for (i in 0 until (content ).count) {
                val bodyPart = content.getBodyPart(i)
                result.addAll(getAttachments(bodyPart)!!)
            }
        }
        return result
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接