download mail attachment with Java

前端 未结 6 861
耶瑟儿~
耶瑟儿~ 2020-12-13 03:29

I had a look in the reference doc, and Spring seems to have pretty good support for sending mail. However, I need to login to a mail account, read the messages, and download

6条回答
  •  忘掉有多难
    2020-12-13 03:45

    I used Apache Commons Mail for this task:

    import java.util.List;
    import javax.activation.DataSource; 
    import javax.mail.internet.MimeMessage;   
    import org.apache.commons.mail.util.MimeMessageParser;   
    
    public List getAttachmentList(MimeMessage message) throws Exception {
        msgParser = new MimeMessageParser(message);
        msgParser.parse();
        return msgParser.getAttachmentList();
    }
    

    From a DataSource object you can retrieve an InputStream (beside name and type) of the attachment (see API: http://docs.oracle.com/javase/6/docs/api/javax/activation/DataSource.html?is-external=true).

提交回复
热议问题