1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

EWS: rebuild broken message (null MimeContent) from properties

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1802 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2011-09-29 21:19:16 +00:00
parent ffb4740fe7
commit 8d2cb827a5
2 changed files with 43 additions and 1 deletions

View File

@ -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;
}

View File

@ -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));