I was working with custom email event handler in SharePoint using SPEmailEventReceiver and found that when using the default functionality of I am able to configure and get the attachments to be added to the document library. Here is how the Incoming Email settings look like when you have your SharePoint server configured to receive emails and you enable your document library to receive emails.
I took the exact code that was provided in the MSDN documentation of SPEmailEventReceiver and implemented in my SharePoint site.
1: namespace Example_Namespace
3: public class Email_Handler: SPEmailEventReceiver
5: public override void EmailReceived(
6: SPList oList,
7: SPEmailMessage oMessage,
8: string strReceiverData)
10: SPListItem oListItem = oList.Items.Add();
11: oListItem["Title"] = oMessage.Headers["Subject"];
12: oListItem["Body"] = oMessage.HtmlBody;
Now when I go to Incoming Email settings page for my Document library, here is what I see.
Where have all the settings gone?
Also when using the sample code from the MSDN article, I find that the attachments I am sending along with the email are not being captured in the document library. As my class is inheriting from SPEmailEventReceiver, my initial thought got me to that if I call base.EmailReceived in my event handler, then SharePoint should be able to handle the attachments and permission checks etc. But no, even calling base.EmailReceived did not resolve the problem.
This had me dive into checking what SharePoint does with the default settings and when I do not have my custom email event handler.
After some digging, found that SharePoint implement its own internal Email handler class which takes care of checking permissions, checking email settings for document library and adding the actual items to the list/library. EmailReceived function for SPEmailEventReceiver is a blank method with no definition. That is why calling "base.EmailReceived" method from class (inheriting from SPEmailEventReceiver) will have no effect.
Which lead to me investigating SPEmailMessage class (object of which is passed to EmailReceived method of your customer email event receiver). It has got Attachments property which contain all the attachments that are there in the email. Now things seems to be more in perspective and finally wrote the code to dump the attachments in the document library.
1: public override void EmailReceived( SPList list, SPEmailMessage message, string receiverData)
3: // Your rest of code to handle the different aspects of the email message received
4: // goes here. This would include permission check, duplicate items etc.
5: // After that, we will call the code to dump all the attachments in the root folder
6: foreach (SPEmailAttachment attachment in message.Attachments)
8: byte attachmentArray = new byte[attachment.ContentStream.Length];
9: attachment.ContentStream.Read(attachmentArray, 0, (int)attachment.ContentStream.Length);
10: list.RootFolder.Files.Add(attachment.FileName, attachmentArray);
Please note sample does not provide any functionality to check list settings for user permissions, duplicate files or saving the original email. This all this done by the default email handler for SharePoint. If you are planning to implement your own EmailHandler, you will need to take care of all the default functionality also provided by SharePoint.