diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 4dd36f35..f8d24bdc 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -36,6 +36,7 @@ import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import javax.mail.MessagingException; +import javax.mail.Session; import javax.mail.internet.MimeMessage; import javax.mail.util.SharedByteArrayInputStream; import java.io.BufferedReader; @@ -496,7 +497,43 @@ public class EwsExchangeSession extends ExchangeSession { executeMethod(getItemMethod); byte[] mimeContent = getItemMethod.getMimeContent(); if (mimeContent == null) { - throw new IOException("GetItem returned null MimeContent"); + LOGGER.warn("GetItem returned null MimeContent, trying to rebuild from properties"); + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + getItemMethod = new GetItemMethod(BaseShape.ID_ONLY, itemId, false); + getItemMethod.addAdditionalProperty(Field.get("contentclass")); + getItemMethod.addAdditionalProperty(Field.get("message-id")); + getItemMethod.addAdditionalProperty(Field.get("from")); + getItemMethod.addAdditionalProperty(Field.get("to")); + getItemMethod.addAdditionalProperty(Field.get("cc")); + getItemMethod.addAdditionalProperty(Field.get("subject")); + getItemMethod.addAdditionalProperty(Field.get("body")); + executeMethod(getItemMethod); + EWSMethod.Item item = getItemMethod.getResponseItem(); + + MimeMessage mimeMessage = new MimeMessage((Session) null); + mimeMessage.addHeader("Content-class", item.get(Field.get("contentclass").getResponseName())); + mimeMessage.addHeader("From", item.get(Field.get("from").getResponseName())); + mimeMessage.addHeader("To", item.get(Field.get("to").getResponseName())); + mimeMessage.addHeader("Cc", item.get(Field.get("cc").getResponseName())); + mimeMessage.setSubject(item.get(Field.get("subject").getResponseName())); + String propertyValue = item.get(Field.get("body").getResponseName()); + mimeMessage.setContent(propertyValue, "text/html"); + + mimeMessage.writeTo(baos); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Rebuilt message content: " + new String(baos.toByteArray())); + } + mimeContent = baos.toByteArray(); + + } catch (IOException e2) { + LOGGER.warn(e2); + } catch (MessagingException e2) { + LOGGER.warn(e2); + } + if (mimeContent == null) { + throw new IOException("GetItem returned null MimeContent"); + } } return mimeContent; } diff --git a/src/java/davmail/exchange/ews/Field.java b/src/java/davmail/exchange/ews/Field.java index a90b03e0..ac9161b0 100644 --- a/src/java/davmail/exchange/ews/Field.java +++ b/src/java/davmail/exchange/ews/Field.java @@ -81,6 +81,11 @@ public final class Field { FIELD_MAP.put("cc", new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, "cc")); FIELD_MAP.put("from", new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, "from")); + FIELD_MAP.put("contentclass", new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, "content-class")); + FIELD_MAP.put("message-id", new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, "message-id")); + + FIELD_MAP.put("body", new UnindexedFieldURI("item:Body")); + // folder FIELD_MAP.put("folderclass", new ExtendedFieldURI(0x3613, ExtendedFieldURI.PropertyType.String));