OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
870 stars 558 forks source link

Email listing does not return all fields #374

Closed douglasheld closed 9 years ago

douglasheld commented 9 years ago

Hi all,

Following the instructions to retrieve a message list does not include essential details like from email From: and To: fields. It seems like the "friendly names" are included, but these of course are not email addresses.

The following code lists my Inbox messages, but the getToRecipients() is always 0 elements; the getFrom().getAddress() is always null; and getInternetMessageHeaders() is also null.

How can I specify the fields I want in the result?

private static void checkInbox( ) throws Exception{
    ExchangeService service = new ExchangeService( ExchangeVersion.Exchange2010_SP2 ); //"14.3.210.2"
    service.setUrl( new URI( "https://webmail.example.com/ews/exchange.asmx" ) );
    service.setCredentials( new WebCredentials( USERNAME, PASSWORD ) );
    Folder inbox = Folder.bind( service, WellKnownFolderName.Inbox );
    ItemView top1000 = new ItemView( 1000 );
    FindItemsResults<Item> result = inbox.findItems( top1000 );
    for ( Iterator<Item> m = result.iterator(); m.hasNext();  ){
        Item peek = m.next();
        if ( peek instanceof EmailMessage ) {
            EmailMessage msg = (EmailMessage) peek;

            System.out.println( "\nMessage   " + msg.getInternetMessageId() );
            System.out.println( "Date:       " + msg.getDateTimeReceived().toGMTString() );
            System.out.println( "From:       " + msg.getFrom().getName() );
            System.out.println( "To:         " + msg.getDisplayTo() );
            System.out.println( "Recipients: " + msg.getToRecipients().getCount() );
            System.out.println( "Subject:    " + msg.getSubject() );

        }
        else 
            System.out.println( "\nEncountered a non-email message." );
    }
}
evpaassen commented 9 years ago

You'll probably need to configure the PropertySet you want to retrieve on the ItemView object.

douglasheld commented 9 years ago

I would appreciate if you could show how that is done. I did not have very good luck with taking out the PropertySet, adding more ItemSchema.* properties to it, and then putting it back in the ItemView.

evpaassen commented 9 years ago

There's an example in the Getting Started Guide.

douglasheld commented 9 years ago

Thank you. That is the original example I was following. I did not have any luck with the following customization of PropertySet to try to return the elements I want (e.g. From: and To: email headers)

I would appreciate if you could show how that is done. I did not have very good luck with this:

...

    PropertySet fields = new PropertySet();
    fields.add( ItemSchema.InternetMessageHeaders );
    top1000.setPropertySet( fields );
    FindItemsResults<Item> result = inbox.findItems( top1000 );

...

serious6 commented 9 years ago

you can use PropertySet fields = new PropertySet(ItemSchema.Id,EmailMessageSchema.From, EmailMessageSchema.ToRecipients) (extend this list with the additional properties you want to load) also a taken hint from msdn

To get the message headers, the EWS schema exposes the InternetMessageHeaders first-class property. Unfortunately, this property does not appear to return a complete set of message headers. For example, in Exchange 2010 Service Pack 1 (SP1), the InternetMessageHeaders property doesn't return address headers such as From and To