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.
PingBack from http://asp-net-hosting.simplynetdev.com/attachments-disappear-with-custom-email-event-handler/
So how did you get the emails that you sent to appear in the document library. What code did you add?
The requirement in this case was to put the attachments from the email into the document library. The code given for the EmailReceived event handler in the post, will do exactly the same.
If you also want to save the whole email message in the document library, you can use SPEmailMessage.GetMessageStream() method and save the stream as a .eml file in the document library.
I'm kind of new to SharePoint EventHandlers. I've tried using the above code to pull the body message and subject into my SharePoint list. Before I install/deploy the eventhandler into the SharePoint document library, I am able to receive emails. But after I have install/deploy the eventhandler into the SharePoint, the document library did not even receive any email. Any idea on whats wrong?
Previously I have posted here, but it seems that it did not display so i'm not sure if it was submitted. Anyways, I'd like to post again hoping to find a solution for this problem that I am stuck with. After I have installed the EventHandler with the above codes, it seems that it does not receive any more email in the document library. I'm really out of ideas on how to work around it or what is the problem.
Thank you so much.
you would need to put a break point in your event handler and see if the event handler is actually being called and what is happening within the event handler.
Let me know if that helps.
Thanks for the reply. I created the code using the 'Class Library' and I'm deploying the source code into a .dll file only. Therefore I don't think the breakpoint works here.
Is there any other way I could test out the source code? Or if there are any templates that I could get from you to run with the code? My e-mail is 'email@example.com'.
I apologize for spamming your blog. I really appreciate your guidance.
You can follow the debugging processes given at http://msdn.microsoft.com/en-us/library/dd206929.aspx or http://www.codeproject.com/KB/sharepoint/SharePoint-Debugging.aspx.
These can help you understand how to debug your SharePoint code. Also ULS logs can give you pointers, if there are some errors in the code.
Sorry for the late reply, I've installed the debugger following the instructions given in the websites that you have referred me to. But I do not really know how to work it out. But from the Event Viewer -> Application this is the error that I got:
Error loading and running event receiver getEmailBody.getEmail in emailBody, Version=22.214.171.124, Culture=neutral, PublicKeyToken=bbcc0774720ac363. Additional information is below.
: To add an item to a document library, use SPFileCollection.Add()
But I have already loaded the assembly into the document folder. Any idea on whats wrong?
Please advice. Thanks.
Send me your project and I can have a quick look.
Good news! I have found out that by using 'Announcement List' I could get all the details from the email. Previously I was using a document library which was a hassle.
Thanks so much for your guidance and time. Hope to keep in touch with you in future. Will drop by your blog from time to time to learn more SharePoint stuffs.
Great stuff you got here. :)
The codes above works well, my issue is that I cannot retrieve the images included in the email message, I think the images is embedded within the email and I cannot find it (so that I can attach it to the list) using: "override EmailRevceived()". Also, I'm having trouble including the sender's active directory account in the [Created By] column of the Annoucements list. Does anyone have solutions to these issues?
If the image is not an attachment in the email but is embedded, you would need to convert the email message to a MIME complaint object which can give you access to embeded resources. A sample MIME parser is available at http://mimeparser.codeplex.com/. I have not tested this but from description, it looks promising.
You can get the raw feed of email message using SPEmailMessage.GetMessageStream method.
To update the Created By column, you cannot just assign a value. You can get more details from Sowmyan's blog at http://blogs.msdn.com/sowmyancs/archive/2008/03/14/can-we-update-the-values-of-created-by-modified-by-columns-in-sharepoint-lists.aspx.
Thanks for this solution, I've implemented it on my sharepoint server.
Now I need some tips regarding the e-mails that are saved on the list. When ever I send a e-mail to the list, the field "created by" is filled with the value "System Account". Is there any way that I can solve this so the name that appears on the "created by" field is the same as the user that sent the e-mail?
I'm trying to use impersonate programatically to solve this but with no luck yet. Do you have any tip for me.
You can follow the Sowmyan's blog at http://blogs.msdn.com/sowmyancs/archive/2008/03/14/can-we-update-the-values-of-created-by-modified-by-columns-in-sharepoint-lists.aspx to see how we can update the "Created By" column.
You would ideally need to do this in the ItemAdded event, so that after the email has been added to the list, you can update the field value.
More details on List Events available at http://msdn.microsoft.com/en-us/library/ms437502.aspx.