3

Environment is Sharepoint 2010. The issue I'm running into is essentially the one that occurred here: http://social.technet.microsoft.com/Forums/en-US/sharepointadmin/thread/f74ca4cb-44fe-4918-b206-b6616458a524 except that I am running a custom email event receiver instead of the native SharePoint one.

The section that handles the attachments happens here:

foreach (SPEmailAttachment attachment in emailMessage.Attachments)
{
    byte[] attachmentArray = new byte[attachment.ContentStream.Length];
    attachment.ContentStream.Read(attachmentArray, 0, (int)attachment.ContentStream.Length);
    item.Attachments.Add(fileName, attachmentArray);
}

This code works fine for every attachment that I've tried, with the exception of .msg files. When any kind of .msg file is attached to an incoming email, no attachments appear and my event receiver logs an error that there were special characters that are not allowed in a file name. Even the most harmlessly named files like "a.msg" gives this error. Has anyone run into this problem with email attachments for an incoming email enabled list?

2
  • Maybe the filetype ist blocked?
    – Sascha
    Commented Feb 22, 2013 at 20:38
  • I checked and unfortunately it is not. The default SharePoint event handler can handle .msg attachments, but I need to attach an event receiver to deal with the files/emails as they come in. Adding an event receiver to "ListItemAdded" does not behave properly.
    – user11457
    Commented Feb 22, 2013 at 20:53

1 Answer 1

1

If anyone else comes across this issue, the fix is quite simple. Emails with other emails as attachments do not have a filename but rather the file name is stored as a header. See my code snippet below for how to access the filename.

string fileName = attachment.FileName;
if (fileName == null)
{
    if (string.Equals(attachment.ContentType, "message/rfc822", StringComparison.OrdinalIgnoreCase))
    {
        fileName = new SPEmailMessage(attachment.ContentStream, (string)null).Headers["subject"] + ".eml";
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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