试图在java中将附加的msg文件保存在本地磁盘中

时间:2015-02-10 11:12:54

标签: java email

我正在使用msgparser在java中阅读outlook msg文件。下面的代码。

MsgParser msgp = new MsgParser();
Message msg = msgp.parseMsg("temp.msg");            
List<Attachment> atts = msg.getAttachments();

for (Attachment att : atts) {
    if (att instanceof FileAttachment) {

    }
    if (att instanceof MsgAttachment){
        MsgAttachment msg1 = (MsgAttachment) att;
        Message msg2 = msg1.getMessage();
    }
}

在上面的代码中我得到了msgAttachment对象。我无法将其写入文件。我希望将其写为outlook msg文件。

1 个答案:

答案 0 :(得分:-1)

我看到你正在使用Java。您可以尝试使用jacob框架。 使用Jacob框架的先决条件是加载jacob dll库。

要阅读邮件和其他邮件属性,您可以浏览该链接 https://msdn.microsoft.com/en-us/library/office/aa210946(v=office.11).aspx

假设 - 您正尝试通过Outlook Client读取邮件。

要存储邮件中的附件,您可以尝试使用以下代码。

ActiveXComponent xl = new ActiveXComponent(
                    "Outlook.Application");
Dispatch explorer = Dispatch
                    .get(xl, "ActiveExplorer").toDispatch();
Dispatch selection = Dispatch.get(explorer,
                    "Selection").toDispatch();
Variant count = Dispatch.get(selection, "Count");
int toInt = count.getInt();
for(int j=1;i<=toInt;i++){
    Dispatch mailItem = Dispatch.call(selection,
          "Item", new Variant(j)).toDispatch();
    Dispatch attachs = Dispatch.get(mailItem, "Attachments").toDispatch();

    Variant count1 = Dispatch.get(attachs, "Count");
    int numberOfAttach = count1.getInt();           
    for (int i = 1; i <= numberOfAttach; i++) {

        Attachment attach = new Attachment();
        Dispatch attachment = Dispatch.call(attachs, "Item",
                new Variant(i)).toDispatch();

        //get the name of the file 
        Variant nameOfFile = Dispatch.get(attachment, "DisplayName");                       
        String displayName = nameOfFile.getString();
        Variant type = Dispatch.get(attachment, "Type");

        Variant saveAttachment = null;
        saveAttachment = Dispatch.call(attachment, "SaveAsFile","path where attachment need to be saved");
    }
}