1

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?

phoenix
  • 795
  • 1
  • 7
  • 14

1 Answers1

0

The StackOverflowError extends the VirtualMachineError class, which indicates that the JVM is broken, or it has run out of resources and cannot operate.

For more information, you could refer to:

java.lang.StackOverflowError – How to solve StackOverflowError

You could add existing item as attachment use below code:

FolderId  folderid= new FolderId(WellKnownFolderName.Inbox,"MailboxName");    
Folder Inbox = Folder.Bind(service,folderid);  
ItemView ivItemView =  new ItemView(1) ;     
FindItemsResults<Item> fiItems = service.FindItems(Inbox.Id,ivItemView);
if(fiItems.Items.Count == 1){  
EmailMessage mail = new EmailMessage(service);   
EmailMessage OriginalEmail = (EmailMessage)fiItems.Items[0];
PropertySet  psPropset= new PropertySet(BasePropertySet.IdOnly);    
psPropset.Add(ItemSchema.MimeContent);
psPropset.Add(ItemSchema.Subject);
OriginalEmail.Load(psPropset);  
ItemAttachment Attachment = mail.Attachments.AddItemAttachment<EmailMessage>();
Attachment.Item.MimeContent = OriginalEmail.MimeContent;  
ExtendedPropertyDefinition PR_Flags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);    
Attachment.Item.SetExtendedProperty(PR_Flags,"1");    
Attachment.Name = OriginalEmail.Subject;  
mail.Subject = "See the Attached Email";  
mail.ToRecipients.Add("glen.scales@domain.com");
mail.SendAndSaveCopy();     

You can’t another message directly, you need to use the MimeContent of the Original Message and then create an ItemAttachment based on that.

Reference to:

Create an email using EWS Exchange and attach another email

Alina Li
  • 884
  • 1
  • 6
  • 5
  • I have already referred to Glen's answer on this question (as I mentioned in my question) and have written my Java code based on it. – phoenix Oct 11 '18 at 15:35
  • You could refer to this link: https://stackoverflow.com/questions/28450984/ews-java-api-get-attachment – Alina Li Oct 16 '18 at 09:11