2

I am trying to make a program that will neatly print all the bodies of the messages of my inbox yet exchange web services is making it difficult. I seem to have easy access to everything except the body of the message. This is what I'm doing right now

static final int SIZE = 10;
public static void main(String [] args) throws Exception {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

ExchangeCredentials credentials = new WebCredentials("USERNAME","PASS");
service.setCredentials(credentials);
service.setUrl(new URI("https://MY_DOMAIN/ews/exchange.asmx"));

ItemView view = new ItemView (SIZE);
FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, view);
System.out.println(findResults.getItems().size() + "Messages");


for (int i = 0; i < SIZE; ++i) {
    try {
        Item item = findResults.getItems().get(i);
    System.out.println("SUBJECT: " + item.getSubject());
    System.out.println("TO: " + item.getDisplayTo());
    System.out.println("BODY: " + item.getBody().toString());


    } catch (IndexOutOfBoundsException e) {
        break;
    }


}

Of course, I have my credentials and domain filled out fittingly for my code. When I run this I get this message though.

Exception in thread "main" microsoft.exchange.webservices.data.ServiceObjectPropertyException: You must load or assign this property before you can read its value.
    at microsoft.exchange.webservices.data.PropertyBag.getPropertyValueOrException(Unknown Source)
    at microsoft.exchange.webservices.data.PropertyBag.getObjectFromPropertyDefinition(Unknown Source)
    at microsoft.exchange.webservices.data.Item.getBody(Unknown Source)
    at Main.main(Main.java:26)

Line 26 is the line where I try to print the body. What am I doing wrong?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
mgild
  • 774
  • 6
  • 13

2 Answers2

2

The FindItem operation doesn't return the Body of a Message so you need to make a separate GetItem Request to the server to get this. In the Managed API you should be able to use Load method to do this so just change

Item item = findResults.getItems().get(i);
item.Load()

Cheers Glen

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • It gives me this Http connection error but still pulls the number of emails in my inbox. Exception in thread "main" microsoft.exchange.webservices.data.EWSHttpException: Connection not established – mgild Jul 09 '14 at 15:28
  • The Connection not established is a know error in the Java API, to fix this you need to patch the code yourself see http://social.msdn.microsoft.com/Forums/en-US/cc018f5f-56ef-43ea-8bee-ce52760589b7/e2010-ewsja-java-windows-server-2003-email-having-null-character-in-subject?forum=exchangesvrdevelopment – Glen Scales Jul 10 '14 at 06:05
  • I went without HTML. it has any method for that ?? – Divyesh Kanzariya Feb 23 '16 at 05:57
1

I've figured it out actually. It looks like ExchangeService will close the connection after it is done pulling the needed info. to fix this I made a function

private static ExchangeService getService() throws Exception {
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
    ExchangeCredentials credentials = new WebCredentials("USERNAME","PASS");
    service.setCredentials(credentials);
    service.setUrl(new URI("DOMAIN"));
    return service;
}

I then call load like so

getService().loadPropertiesForItems(findResults, itempropertyset);

Where I define itempropertyset as such

PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
itempropertyset.setRequestedBodyType(BodyType.Text);
view.setPropertySet(itempropertyset);
mgild
  • 774
  • 6
  • 13