Jacob Export Outlook联系图片(Java,Jacob)

时间:2014-02-16 08:01:05

标签: java outlook jacob

早上好,

我正在使用Jacob 1.17 o阅读我所有的Outlook联系人图片并将其保存到文件中。对于前199个Contatcs,该程序运行良好。之后,Dispatch.call失败并终止,并出现以下异常:

Exception in thread "main" com.jacob.com.ComFailException: Invoke of: SaveAsFile
Source: Microsoft Outlook
Description: Cannot save the attachment. Cannot create file: ContactPicture.jpg.
Right-click the folder you want to create the file in, and then click Properties on
the shortcut menu to check your permissions for the folder.

    at com.jacob.com.Dispatch.invokev(Native Method)
    at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
    at com.jacob.com.Dispatch.callN(Dispatch.java:453)
    at com.jacob.com.Dispatch.call(Dispatch.java:541)
    at outlookStuff.ManageContactsOutlook.tmpTest(ManageContactsOutlook.java:217)
    at mainPackage.Main.main(Main.java:32)

我真的不确定。我测试了一组不同的联系人 - 相同的错误。将所有对象设置为null以使垃圾收集器参与其中但它没有帮助。

造成麻烦的代码:

public void tmpTest(int intOutlookFolder, String strWorkingDir) {
Dispatch dipNamespace = this.axc.getProperty("Session").toDispatch();
Dispatch dipContactsFolder = Dispatch.call(dipNamespace, "GetDefaultFolder", (Object) new Integer(intOutlookFolder)).toDispatch();
Dispatch dipContactItems = Dispatch.get(dipContactsFolder, "items").toDispatch();

    @SuppressWarnings("deprecation")
    int count = Dispatch.call(dipContactItems, "Count").toInt();

    for (int i=1; i<=count; i++) {
        Dispatch dipContact;            
        dipContact = Dispatch.call(dipContactItems, "Item", new Integer(i)).toDispatch();

        String strEntryID = Dispatch.get(dipContact, "EntryID").toString().trim();

        //For Testing
        Status.printStatusToConsole("Outlook Contact "+strEntryID+" loaded");

        byte[] byteContactPicture = null;
        String strPathToTmpPicture = null;
        Dispatch dipAttachments = Dispatch.get(dipContact, "Attachments").toDispatch();
        @SuppressWarnings("deprecation")
        int countAttachements = Dispatch.call((Dispatch) dipAttachments, "Count").toInt();
        for (int j=1; j<=countAttachements; j++) {
            Dispatch currentAttachement;            
            currentAttachement = Dispatch.call(dipAttachments, "Item", new Integer(j)).toDispatch();

            if (Dispatch.get(currentAttachement, "FileName").toString().equals("ContactPicture.jpg")) {                 
                strPathToTmpPicture = strWorkingDir+strEntryID+".jpg";

                //The Crashing Part
                Dispatch.call(currentAttachement, "SaveAsFile", strPathToTmpPicture);

                File tmpFile = new File(strPathToTmpPicture);
                if (tmpFile.exists()) {
                    try {
                        byteContactPicture = org.apache.commons.io.FileUtils.readFileToByteArray(tmpFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                currentAttachement = null;
                tmpFile = null;
            }
            currentAttachement = null;
        }
        dipAttachments = null;
    }
    dipContactItems = null;
    dipContactsFolder = null;
    dipNamespace = null;
}

愿某人有想法吗?

由于 航空

0 个答案:

没有答案