I've been fighting this problem for some time now, and have failed to find an answer online that works. I am using the Exchange EWS API to do some email processing. One of the things I need to process is an EmailMessage that has attachments on it. One of those attachments happens to be another EmailMessage. I will refer to this as the attached EmailMessage.
I want to convert this EmailMessage to a byte[], however every time I try, I get an Exception. Below is my code:
if (((ItemAttachment)attachment).Item is EmailMessage)
{
EmailMessage msg = ((ItemAttachment)attachment).Item as EmailMessage;
msg.Load(new PropertySet(ItemSchema.MimeContent));
byte[] content = msg.MimeContent.Content;
}
The problem is no matter what I try to load, I get an exception thrown saying
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Exchange.WebServices.dll but was not handled in user code
Additional information: This operation isn't supported on attachments.
If I don't call msg.Load(), I get a different error saying I need to load the content.
I don't understand this. If I do the same operation on an EmailMessage that was not attached to anything, it works just fine. Why does it matter that the EmailMessage was an attachment at one point in time? How can I get the EWS/.NET/Whatever is throwing the Exception to treat the attached EmailMessage as an EmailMessage and not an ItemAttachment?