2015-02-10 57 views
0

我正在阅读使用msgparser在Java中的Outlook味精文件。下面的代码。试图保存本地磁盘中的附件味精文件在java

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

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

为了读取邮件和其他邮件属性你可以通过链接 https://msdn.microsoft.com/en-us/library/office/aa210946(v=office.11).aspx

假设下进行 - 您正在尝试通过读取Outlook客户端邮件。

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

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"); 
    } 
}