Search code examples
javaexchangewebservicesewsjavaapi

How to fetch "To" information from email using ews in java


I need your help in Fetching TO information from the mail using Java.

I have got C# code, but dont know how to write into Java. For reference I am placing C# code below here.

Recipients = ((Microsoft.Exchange.WebServices.Data.EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray().

It would be great if I can see this code in java.

Thanks in advance.


Solution

  • If the only property you want to read is ToRecipients (precisely EmailMessageSchema.ToRecipients) you can do this like that:

        PropertySet propertySet = new PropertySet(EmailMessageSchema.ToRecipients);
        EmailMessage email = EmailMessage.bind(service, new ItemId(emailId), propertySet);
        EmailAddressCollection toRecipients = email.getToRecipients();
        for (EmailAddress toRecipient : toRecipients) {
            String address = toRecipient.getAddress();
            // go on
        }
    

    Providing a propertySet like above will enusre that the property ToRecipients will be the only one set on the returned EmailMessage. Thus the call isn't that expensive like:

    EmailMessage email = EmailMessage.bind(service, new ItemId(emailId));
    

    This would return an EmailMessage with all first class properties set. ToRecipients is a member of them.

    EDIT:
    Caution: There is also the property ItemSchema.DisplayTo. So asking in the title of the question for "To" is ambiguous.