2
Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
MAPIFolder Folder_Contacts;
Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;

for (int i = 0; i < OutlookItems.Count; i++)
{
    Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];

    bool to = contact.HasPicture; //returns false although there is a picture in Outlook.
    string h = contact.FirstName; //returns my first name
}

Why is the above code not able to see the picture but can pull the first name and how can I get the picture.

Error:

enter image description here

enter image description here

Si8
  • 9,141
  • 22
  • 109
  • 221
  • Try here: http://stackoverflow.com/questions/4920168/how-to-programmatically-get-a-contacts-picture-in-outlook-2010 – Sorceri Mar 09 '16 at 21:47

2 Answers2

1

Attachment.GetTemporaryFilePath Method (Outlook)

var attachment = contact.Attachments["ContactPicture.jpg"] as Attachment;
var path = attachment.GetTemporaryFilePath();

Attachment.SaveAsFile Method (Outlook)

var attachment = contact.Attachments["ContactPicture.jpg"] as Attachment;
attachment.SaveAsFile(savePath);

Note that "path" here includes the file name, not just the directory.

Example:

var savePath = @"C:\Users\User\Documents\OutlookPhotos\YourDesiredFileName.jpg";

Edit:

Here's a method. It handles nulls for you.

static void WriteOutlookContactPhotoToFile(ContactItem contact, string directory, string fileName)
{
    if (contact != null && contact.HasPicture)
    {
        var attachment = contact.Attachments["ContactPicture.jpg"] as Attachment;
        if (attachment != null)
        {
            string writePath = Path.Combine(directory, fileName);
            attachment.SaveAsFile(writePath);
        }
    }
}
Frozenthia
  • 759
  • 3
  • 9
  • Thanks but the bool remains false so how can I get an attachment? – Si8 Mar 10 '16 at 04:22
  • @SiKni8 Which boolean? If it's null or has no picture, then you can't get it. I tested this on my own Outlook instance. – Frozenthia Mar 10 '16 at 04:27
  • My outlook account, I do have a thumbnail and I can see it when I send/receive emails. – Si8 Mar 10 '16 at 14:38
  • I get an error: `var path = attachment.GetTemporaryFilePath();`... Error: `Object reference not set to an instance of an object.` – Si8 Mar 10 '16 at 14:41
  • @Sikni8 I had some problems with using `GetTemporaryFilePath()` but iterating over the contact items worked for me. Can you tell me which evaluation remained false? You could iterate over the attachments and see if the indexer is different for you, but "ContactPicture" worked for me. – Frozenthia Mar 10 '16 at 15:03
  • It is null for me regardless so I am wondering if our Outlook setup is different. – Si8 Mar 10 '16 at 15:14
  • How many attachments do you have on the contact item? I found documentation that states that the display image is always the first attachment in the collection. – Frozenthia Mar 10 '16 at 15:19
  • `OutlookItems.Count` = `2` – Si8 Mar 10 '16 at 16:23
  • @SiKni8 In your screenshot, `HasPicture` is false, and you're not checking for nulls in the attachment, which it would be since `HasPicture` is false. That's the reason the exception is thrown. I recommend looking in your temp directory and seeing if you can locate that image, and then implementing something similar to what Sorceri gave you in a comment to your question. – Frozenthia Mar 10 '16 at 16:45
  • I understand why the exception is thrown as `HasPicture` is false. Is the above code retrieving using the Temp directory? I looked in the Temp folder but didn't see a "ContactPicture.jpg" file. – Si8 Mar 10 '16 at 17:00
  • @SiKni8 It won't have that file name. It might be the contact's `EntryID` property + ".jpg". It's extremely odd that `HasPicture` would be false at all, regardless of your Outlook set up. If you can locate the image, you can iterate over your collection and look it up by the EntryID and simply read and write it elsewhere. – Frozenthia Mar 10 '16 at 17:04
  • It is weird, I don't see it under my Windows profile. Oh well, guess I won't be able to get the picture. Thanks anyway :) – Si8 Mar 10 '16 at 17:28
0

In your screenshot above, ContactItem.HasPicture == false, which means there is no picture. The attachment is not guaranteed to be present. you need to check for null (attachment != null).

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I am the logged in user in Outlook and I do have a thumbnail of me. – Si8 Mar 10 '16 at 21:04
  • Coming from where? From a contact in your default Contacts folder (that is what your code is retrieving)? From GAL on the Exchange server? Or the picture you manually set in the Control Panel Users applet? – Dmitry Streblechenko Mar 10 '16 at 21:53