如何获得outlook 2010中最新的联系人图片?

时间:2017-04-19 09:39:15

标签: c# outlook-addin

问题在于:

  1. 使用Attachment.SaveAsFile()将联系人的图片保存在磁盘上。(已成功)
  2. 手动更改Outlook中联系人的图片。
  3. 重复步骤1,但我得到旧图片,而不是第2步中的新图片。
  4. 编辑: 我知道如何在磁盘上保存联系人的图片。 问题是我得到的图片不是最新图片。 这是代码:

    //[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
            Outlook.Application outlook = new Outlook.Application();
            Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            foreach (var item in folder.Items) {
                if (item is Outlook.ContactItem) {
                    Outlook.ContactItem contact = null;
                    Outlook.Attachments atts = null;
                    Outlook.Attachment att = null;
                    string path = "";
    
                    contact = item as Outlook.ContactItem;
    
                    if (!contact.HasPicture) { continue; }
    
                    path = @"C:\Temp\" + contact.EntryID + ".jpg";
                    atts = contact.Attachments;
                    att = atts["ContactPicture.jpg"];
    
                    if(File.Exists(path)){
                        File.Delete(path);
                    }
    
                    att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");
    
                    Marshal.ReleaseComObject(att);
                    att = null;
                    Marshal.ReleaseComObject(atts);
                    atts = null;
                    Marshal.ReleaseComObject(contact);
                    contact = null;
                }
            }
    

    谢谢!

1 个答案:

答案 0 :(得分:0)

我在Contact picture retrieved via PIA doesn't change when updated in Outlook

上找到了解决方案

我没有发布folder.Items

以下是修改后的代码:

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
        Outlook.Application outlook = new Outlook.Application();
        Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
        Outlook.Items items = null;
        items = folder.Items;
        foreach (var item in items) {
            if (item is Outlook.ContactItem) {
                Outlook.ContactItem contact = null;
                Outlook.Attachments atts = null;
                Outlook.Attachment att = null;
                string path = "";

                contact = item as Outlook.ContactItem;

                if (!contact.HasPicture) { continue; }

                path = @"C:\Temp\" + contact.EntryID + ".jpg";
                atts = contact.Attachments;
                att = atts["ContactPicture.jpg"];

                if (File.Exists(path)) {
                    File.Delete(path);
                }

                att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");

                Marshal.ReleaseComObject(att);
                att = null;
                Marshal.ReleaseComObject(atts);
                atts = null;
                Marshal.ReleaseComObject(contact);
                contact = null;
            }
        }

        if (items != null) {
            Marshal.ReleaseComObject(items);
            items = null;
        }

编辑:

上述代码出现问题。有时它得到了旧图片。 我最后添加了GC.Collect();,然后效果很好。

所以我尝试了以下代码。但问题仍然存在。

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
        Outlook.Application outlook = new Outlook.Application();
        Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
        Outlook.Items items = folder.Items;

        foreach (var item in items) {
            if (item is Outlook.ContactItem) {
                Outlook.ContactItem contact = item as Outlook.ContactItem;
                string path = "";
                Outlook.Attachment att = null;

                if (!contact.HasPicture) { continue; }

                path = @"C:\Temp\" + contact.EntryID + ".jpg";
                att = contact.Attachments["ContactPicture.jpg"];

                if (File.Exists(path)) {
                    File.Delete(path);
                }

                att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");
            }
        }

        GC.Collect();

最后我使用以下代码。释放所有com对象,然后调用GC.Collect()。

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
        Outlook.Application outlook = new Outlook.Application();
        Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
        Outlook.Items items = folder.Items;

        foreach (var item in items) {
            if (item is Outlook.ContactItem) {
                Outlook.ContactItem contact = null;
                Outlook.Attachments atts = null;
                Outlook.Attachment att = null;
                string path = "";

                contact = item as Outlook.ContactItem;

                if (!contact.HasPicture) { continue; }

                path = @"C:\Temp\" + contact.EntryID + ".jpg";
                atts = contact.Attachments;
                att = atts["ContactPicture.jpg"];

                if (File.Exists(path)) {
                    File.Delete(path);
                }

                att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");

                Marshal.ReleaseComObject(att);
                att = null;
                Marshal.ReleaseComObject(atts);
                atts = null;
                Marshal.ReleaseComObject(contact);
                contact = null;
            }
        }

        if (items != null) {
            Marshal.ReleaseComObject(items);
            items = null;
        }

        Marshal.ReleaseComObject(folder);
        folder = null;

        Marshal.ReleaseComObject(outlook);
        outlook = null;

        GC.Collect();