0

I tried to build an EmailEventReceiver in SharePoint2013 onpremise to handle Mails to a custom list. It works like expected.

But in the UAT I found out, that there could be mailmessages which contain inline elements like images, which are not found in the SPEmailMessage Object.

After some researching I found that those are Mime objects with the Attribute "Content-Disposition: inline". (MIME Message Containing Inline and Non-Inline Attachments) The Message Body itself contains an html reference to the item with something like:

<img width=\"315\" height=\"74\" id=\"Grafik_x0020_1\" src=\"cid:image001.png@01D16AF9.5B9000A0\">

As long as I take the whole MessageStream (SPEMailMessage.GetMessageStream) and save it into a file with the extension .eml I can open the original message in Outlook and the images are shown correctly.

But with accessing SPEMailMessage.HtmlBody or with SPEMailMessage.Headers I can`t find that inline element.

I can save every element that is part of the regular attachments (not inline):

foreach(var attachement in emailMessage.Attachments.Cast<SPEmailAttachment>().Where(attachement => attachement.ContentStream != null))
{
 using (var streamReader = new MemoryStream())
 {
  attachement.ContentStream.CopyTo(streamReader)
  var fileName = RemoveInvalidCharacters(attachement.FileName);
  newItem.Attachments.Add(fileName, streamReader.ToArray());
 }
}

If I try to produce such an inline element, I can do that with code by creating a System.Net.Mail.AlternateView and adding a LinkedResource (e.G. Embed images in a mail message using C#).

But I also couldn`t find any Alternateview in the SPEmailMessage Object.

Any Ideas how I could access that data in an EmailEventReceiver in order to create according Images in SharePoint?

0

After opening a Case with Microsoft Dev Support I learned some interesting things about Emails and SharePoint.

First of all, there are even more "hidden" functions in SharePoint which are not accessible as a .Net / SharePoint Developer than you might think.

Inline Attachements or Elements which have the value "Content-Disposition: inline" are handled by some rather old code parts in SharePoint in the default Email Event receiver.

But this only works in Default Lists from the type of

  • Announcements
  • Discussion Board
  • Calender

Any other list type ignores all inline elements when it creates the mail message dictionary which is accessible as "message.attachments".

What if you create your own Email Event receiver? Well that internal code is also ignored and you don`t have any chance to access those elements.

Microsoft suggested two different solutions.

The first was to use a third party MimeParser and cast the SPEmailEventReceiver to a MimeType compatible class and access the inline Elements directly (e.G. like the MimeParser or the RxMailMessage)

The second was to use one of the three lists as some kind of hidden mailbox and copy the received mails from there (via event receiver or workflow) to their working destination.

We choose to take the second approach and hopefully time will tell if all attachments (inline or not) are going to be found in SharePoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.