从MimeMessage中删除附件

时间:2013-12-19 09:16:47

标签: java multipart javax.mail mime-message

我正在从本地磁盘读取eml文件:

InputStream is = new FileInputStream(body_path);
MimeMessage message = new MimeMessage(session,is);
Multipart multipart = (Multipart)message.getContent();

现在我如何删除" .dat"的附件?类型?

1 个答案:

答案 0 :(得分:0)

您可以这样做: -

撰写new mimemsg并逐个添加所有部分,但扩展名为DAT

的部分除外

撰写新讯息: -

MimeMessage newMimeMsg;
MimeMultipart content;

if (mimemsg.isMimeType("multipart/*")) {
     Multipart multipartmsg = (Multipart) mimemsg.getContent();

for (int i = 0; i < multipartmsg.getCount(); ++i) {
MimeBodyPart bodypart = (MimeBodyPart) multipartmsg.getBodyPart(i);
if(getFileExtension(bodypart.getFileName()).equalsIgnoreCase("dat") {
    // Do Nothing
} else {
    content.addBodyPart(bodypart);
}
}
newMimeMsg.setContent(content);
} else {
   newMimeMsg = mimemsg;
}

public static String getFileExtension(String docFileName) {
        String[] tokens = docFileName.split("\\.(?=[^\\.]+$)");
        return tokens[1];
    }