I am using ews-java-api to create a new email message with an existing item as attachment. I was following Glen's answer to this question to write similar code in java. However, I am getting error - "Items of type EmailMessage are not supported as attachment"
Here is my code snippet for reference:
EmailMessage approvalMessage = new EmailMessage(exchangeService);
ItemAttachment itemAttachment = approvalMessage.getAttachments().addItemAttachment(EmailMessage.class);
MimeContent mimeContent = ewsItem.getMimeContent();
Item attachedItem = itemAttachment.getItem();
attachedItem.setMimeContent(mimeContent);
ExtendedPropertyDefinition prFlags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
attachedItem.setExtendedProperty(prFlags, "1");
itemAttachment.setName(subject);
approvalMessage.setSubject("Reminder Email with original email as attachment");
approvalMessage.getToRecipients().add("my-email-address");
approvalMessage.send();
- approvalMessage is the new email message I am creating
- ewsItem is an existing Item which I want to add as attachment to approvalMessage
Question: Is this some issue with ews-java-api that it doesn't accept EmailMessage as attachment type?
What I tried: I replaced ItemAttachment itemAttachment = approvalMessage.getAttachments().addItemAttachment(EmailMessage.class);
with ItemAttachment itemAttachment = approvalMessage.getAttachments().addItemAttachment(Item.class);
Post this change, I no longer observe above error. However, the code now throws a stackoverflow error at following line - attachedItem.setMimeContent(mimeContent);
Error stack trace-
Oct 09, 2018 9:02:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path []
threw exception [Handler dispatch failed; nested exception is
java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError
at microsoft.exchange.webservices.data.core.PropertyBag.changed
(PropertyBag.java:364)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
itemChanged(ItemAttachment.java:96)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
serviceObjectChanged(ItemAttachment.java:252)
at microsoft.exchange.webservices.data.core.service.ServiceObject.
changed(ServiceObject.java:85)
at microsoft.exchange.webservices.data.core.PropertyBag.changed
(PropertyBag.java:364)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
itemChanged(ItemAttachment.java:96)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
serviceObjectChanged(ItemAttachment.java:252)
at microsoft.exchange.webservices.data.core.service.ServiceObject.
changed(ServiceObject.java:85)
Question: Why does specifying Item as attachment class results in stackoverflow error? What is the right way to add existing item as attachment?