javaexchangewebservicesindependentsoft

How to retrieve Message and Task body - Exchange Web Services - Java


I am creating a message using JWebServices, but even though all other fields are retrieved successfully, body does not. message.getBody() returns null. Here are the two methods I call subsequently.

private void createMessage(Service service) throws ParseException {
    try {
        Message message = new Message();
        message.setItemClass(ItemClass.MESSAGE);
        message.setSubject("Test");
        message.setBody(new Body("Body text"));
        message.getToRecipients().add(new Mailbox("John@mydomain.com"));
        message.getCcRecipients().add(new Mailbox("Mark@mydomain.com"));

        ItemId itemId = service.createItem(message,StandardFolder.SENT_ITEMS);
    } catch (ServiceException e) {
        System.out.println(e.getMessage());
        System.out.println(e.getXmlMessage());

        e.printStackTrace();
    }
}

private void listItemsInSent(Service service) throws ParseException {
    try {
        FindItemResponse response = service.findItem(StandardFolder.SENT_ITEMS);
        Message m = null;
        for (int i = 0; i < response.getItems().size(); i++) {
            m = (Message)response.getItems().get(i);
            System.out.println(m.getSubject());
            System.out.println(m.getItemClass());
            System.out.println(m.getLastModifiedTime());
            System.out.println(m.getBody());
            System.out.println(m.getBodyHtmlText());
            System.out.println(m.getBodyPlainText());
            System.out.println(m.getItemId());
            System.out.println(m.toString());
            System.out.println();
        }
    } catch (ServiceException e) {
        System.out.println(e.getMessage());
        System.out.println(e.getXmlMessage());

        e.printStackTrace();
    }
}

Solution

  • Try to replace

    m = (Message)response.getItems().get(i);
    

    with

    m = service.getMessage(response.getItems().get(i).getItemId());