javamail还提取封装的消息的附件Content-Type:message/rfc822 [英] javamail also extract attachments of encapsulated message Content-Type: message/rfc822

查看:329
本文介绍了javamail还提取封装的消息的附件Content-Type:message/rfc822的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取.eml消息的所有附件,这些消息都封装在InputStream消息中(内容类型:message/rfc822)

I want to extract ALL the attachments of an .eml message wich is encapsulated (Content-Type: message/rfc822) in the message InputStream

InputStream is = new FileInputStream(Path);
MimeMessage mime = new MimeMessage(null, is);



 private  String getAttachments(p) throws
                MessagingException, IOException {


if ( p.isMimeType("multipart/*")) {
                    Multipart multiPart = (Multipart) p.getContent();
                    int numberOfParts = multiPart.getCount();
                    for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
String disp = part.getDisposition();
 if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)  {
    file_name =   part.getFileName();
    part.saveFile(Attachments_Folder + "\\" + MailFileName + "_" + file_name);
     }
     }
  }
}
is.close()

此外,当Content-Type为message/rfc822时,part.getFileName()为null,因此保存的文件没有扩展名,我不知道如何获取此文件.

Also, when the Content-Type is message/rfc822, the part.getFileName() is null and therefore the saved file has no extension and i don't know how to get this one.

推荐答案

我是通过将新文件名作为.eml文件添加到包含的消息中,并使用包含的消息进行递归

I did it by adding a new filename to the included message as an .eml file and a recursion with the included message

import java.util.*;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import java.io.FileInputStream.*;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Part;
import javax.mail.Multipart;

getAttachments(Path) ;
  //function
private  String getAttachments(path) throws
    MessagingException, IOException {
    InputStream is = new FileInputStream(path);
    MimeMessage p = new MimeMessage(null, is);

    if ( p.isMimeType("multipart/*")) {
// if (contentType.contains("multipart")) {
                    Multipart multiPart = (Multipart) p.getContent();
                    int numberOfParts = multiPart.getCount();
                    for (int partCount = 0; partCount < numberOfParts; partCount++) {
            MimeBodyPart part = (MimeBodyPart)multiPart.getBodyPart(partCount);
            String disp = part.getDisposition();

            if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)) {
            file_name =  part.contentType == "message/rfc822" ? "message_inclus" + partCount + ".eml" :             MimeUtility.decodeText(part.getFileName());
            exportedpath = Attachments_Folder + "/" + MailFileName + "_" + file_name; 
            part.saveFile(exportedpath);
            if ( part.contentType == "message/rfc822" ) {
            getAttachments(exportedpath) 
            }
            }
        }
    }
    is.close() 
    return 1
    }

这篇关于javamail还提取封装的消息的附件Content-Type:message/rfc822的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆