How to set the contact Email1DisplayName using the EWS Java API (Exchange Web Service)?

StackOverflow https://stackoverflow.com/questions/16665590

  •  30-05-2022
  •  | 
  •  

Question

I just figured out how to set the title (see How to set the contact title using the EWS Java API (Exchange Web Service)?). Now I am trying to set the email 1 display name.

If I use the exposed API Contact.getEmailAddresses().setEmailAddress(), the display name is automatically set to be the same as the email address (and it overrides my extended properties).

So now I am trying to set the complete email information via extended properties. It almost works, except when I look at the address book, the name and display name are empty.

I have a feeling that this is related to the Email1OriginalEntryId property, which I do not know how to set correctly.

Any ideas?

My current attempt looks like this:

ExtendedPropertyDefinition propDef_PidLidEmail1DisplayName = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8080, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1AddressType = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8082, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1EmailAddress = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8083, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1OriginalDisplayName = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8084, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1OriginalEntryId = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8085, MapiPropertyType.Binary);

ExchangeService mailbox = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
mailbox.setUrl(new URL("https://remote.domain.com/EWS/exchange.asmx").toURI());
ExchangeCredentials credentials = new WebCredentials("user.name", "pw", "domain");
mailbox.setCredentials(credentials);

Contact c = new Contact(mailbox);
c.setGivenName("GivenName");
c.setSurname("Surname");

//    c.getEmailAddresses().setEmailAddress(EmailAddressKey.EmailAddress1, new EmailAddress("AB12@B12.com"));

c.setExtendedProperty(propDef_PidLidEmail1AddressType, "SMTP");
c.setExtendedProperty(propDef_PidLidEmail1EmailAddress, "A12@B12.com");
c.setExtendedProperty(propDef_PidLidEmail1OriginalDisplayName, "A12@B12.com");
c.setExtendedProperty(propDef_PidLidEmail1DisplayName, "A12 B12 (A12@B12.com)");
//    c.setExtendedProperty(propDef_PidLidEmail1OriginalEntryId, ???);

c.save(WellKnownFolderName.Contacts);

enter image description here

Était-ce utile?

La solution

Hard to believe, but after almost a week of fighting with this, I finally figured it out. Tested only on Exchange 2007.

Note that this only works if you set every single extended property like in this example and do not use Contact.getEmailAddresses().setEmailAddress().

ExtendedPropertyDefinition propDef_PidLidEmail1DisplayName = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8080, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1AddressType = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8082, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1EmailAddress = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8083, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1OriginalDisplayName = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8084, MapiPropertyType.String);

ExtendedPropertyDefinition propDef_PidLidEmail1OriginalEntryId = new ExtendedPropertyDefinition(//
    UUID.fromString("00062004-0000-0000-C000-000000000046"), 0x8085, MapiPropertyType.Binary);

ExchangeService mailbox = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
mailbox.setUrl(new URL("https://remote.domain.com/EWS/exchange.asmx").toURI());
ExchangeCredentials credentials = new WebCredentials("user.name", "pw", "domain");
mailbox.setCredentials(credentials);

String FIRST = "First";
String LAST = "Last";
String FIRST_LAST = FIRST + " " + LAST; // "First Last"
String EMAIL = "first.last@email.com";
String DISPLAY_NAME = FIRST + " " + LAST + " (" + EMAIL + ")"; // "First Last (first.last@email.com)"

Contact c = new Contact(mailbox);
c.setGivenName(FIRST);
c.setSurname(LAST);
c.setFileAs(FIRST_LAST);

// don't use this
//    c.getEmailAddresses().setEmailAddress(EmailAddressKey.EmailAddress1, new EmailAddress(EMAIL));

// Address book Name (seem to trigger the whole address book functionality)
c.setSubject(FIRST_LAST);
// Address book email address
c.setExtendedProperty(propDef_PidLidEmail1OriginalDisplayName, EMAIL);
// contact and address book display name
c.setExtendedProperty(propDef_PidLidEmail1DisplayName, DISPLAY_NAME);

c.setExtendedProperty(propDef_PidLidEmail1AddressType, "SMTP"); // constant
c.setExtendedProperty(propDef_PidLidEmail1EmailAddress, EMAIL);

// not needed after all, exchange sets this automatically
//    c.setExtendedProperty(propDef_PidLidEmail1OriginalEntryId, ???);

c.save(WellKnownFolderName.Contacts);

for(Item item : mailbox.findItems(WellKnownFolderName.Contacts, new ItemView(1000)))
{
  Contact result = (Contact) item;

  PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties);
  propertySet.add(propDef_PidLidEmail1AddressType);
  propertySet.add(propDef_PidLidEmail1EmailAddress);
  propertySet.add(propDef_PidLidEmail1OriginalDisplayName);
  propertySet.add(propDef_PidLidEmail1DisplayName);
  propertySet.add(propDef_PidLidEmail1OriginalEntryId);

  result = Contact.bind(mailbox, result.getId(), propertySet);

  LOGGER.info("count: " + result.getExtendedProperties().getCount());

  for(ExtendedProperty p : result.getExtendedProperties())
  {
    LOGGER.info(p.toString());
  }
}

enter image description here

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top