Download attachments using Java Mail

前端 未结 4 1650
自闭症患者
自闭症患者 2020-11-28 03:36

Now that I`ve downloaded all the messages, and store them to

Message[] temp;

How do I get the list of attachments for each of those message

4条回答
  •  暖寄归人
    2020-11-28 03:53

    You can simply use Apache Commons Mail API MimeMessageParser - getAttachmentList() along Commons IO and 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
        }
    }
    

提交回复
热议问题