0

Based on this tutorial, I'm trying to get contacts photos

private String createPhoto() {
    try {
        AttachmentCollection attachments = contact.getAttachments();
        for (Attachment attachment : attachments.getItems()) {
            if (attachment instanceof FileAttachment) {
                boolean isPhoto = ((FileAttachment) attachment).isContactPhoto();
                if (isPhoto) {
                    attachment.load();
                    FileAttachment photo = contact.getContactPictureAttachment();

                    String filename = photo.getName() + ".jpg";
                    photo.load(new FileOutputStream(filename, true));
                    return filename;
                }
            }
        }
    } catch (Exception ex) {
        LOGGER.info("" + ex);
    }
    return null;
}

However, attachments.getItems() is always an empty array.

On my mailbox, I have few contacts with photos, and I can receive them by calling URL https://companyname.exchange.com/EWS/Exchange.asmx/s/GetUserPhoto?email=name@company.exchange.com&size=HR360x360

Why I can't get a photo from the code?

Maja
  • 17
  • 1
  • 6

1 Answers1

0

On my mailbox, I have few contacts with photos, and I can receive them by calling URL https://companyname.exchange.com/EWS/Exchange.asmx/s/GetUserPhoto?email=name@company.exchange.com&size=HR360x360

That request gets the Userphoto which is stored in the (Source)Users Mailbox (or low res in ActiveDirectory) and made available by that operation.

Your code is trying to retrieve the ContactPhoto which can be stored as an attachment on Contacts in a UserMailbox.

So these are two separate things so which one are you dealing with ?, As you haven't shown it you need to make sure you ExchangeServerRequest Version is set to 2010 or greater as Contact photos aren't returned in 2007. You might also want to test quickly the contacts in Question with the EWS Editor https://ewseditor.codeplex.com/ that will allow you to get the objects and see if there is a ContactPhoto Attachments using EWS.

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • thank you for your comment. Exchange version is set to Exchange2010_SP2. EWS editor shows _HasAttachments==false_ When the contact photo is stored as an attachment, or how can I create it? – Maja Dec 01 '16 at 12:13
  • HasAttachments will be false because contact photos are hidden attachments. You should still be able enumerate the attachment using the EWSEditor which will tell you right away if there is an attachment in the first place. If there is no Contact picture then there is no picture you would need to add an new attachment and set the contact photo property to true – Glen Scales Dec 02 '16 at 04:37