outlook 2010如何获取最新的联系人头像?

How to get the newest contact's picture in outlook 2010?

问题是:

  1. 使用 Attachment.SaveAsFile() 将联系人的图片保存在磁盘上。(成功)
  2. 手动更改联系人在 outlook 中的图片。
  3. 重复步骤 1,但我在步骤 2 中得到的是旧图片,而不是新图片。

编辑: 我知道如何将联系人的图片保存在磁盘上。 问题是我得到的图片不是最新的。 这是代码:

//[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;
            }
        }

谢谢!

我在 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();