2
votes

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?

2

2 Answers

2
votes

You need to use the GetAttachments operations on Each on the Embedded Attachments with a propertyset that includes the MimeContent. eg something like (this can made a lot more effienct by grouping the GetAttachment requests if your processing multiple message etc).

            PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
        psPropSet.Add(ItemSchema.MimeContent);
        foreach (Attachment attachment in CurrentMessage.Attachments)
        {
            if (attachment is ItemAttachment)
            {
                attachment.Load();
                if (((ItemAttachment)attachment).Item is EmailMessage)
                {
                    EmailMessage ebMessage = ((ItemAttachment)attachment).Item as EmailMessage;
                    foreach (Attachment ebAttachment in ebMessage.Attachments)
                    {
                        if (ebAttachment is ItemAttachment)
                        {
                            Attachment[] LoadAttachments = new Attachment[1];
                            LoadAttachments[0] = ebAttachment;
                            ServiceResponseCollection<GetAttachmentResponse> getAttachmentresps = service.GetAttachments(LoadAttachments, BodyType.HTML, psPropSet);
                            foreach (GetAttachmentResponse grResp in getAttachmentresps)
                            {
                                EmailMessage msg = ((ItemAttachment)grResp.Attachment).Item as EmailMessage;
                                msg.Load(new PropertySet(ItemSchema.MimeContent));
                                byte[] content = msg.MimeContent.Content;
                            }
                        }
                    }

                }
            }
        }
0
votes

You have to load the attachment with the flag to load MimeContent:

{

if (attachment is ItemAttachment ia)
        {
            ia.Load(ItemSchema.MimeContent);
        }

}