Hello All.
In one of our SharePoint 2010 projects we have a requirement for a custom list to receive an email and the message from the email should be updated in one of the list columns.
The incoming e-mail settings option is enabled under the Communications column in the list settings.
But by default this option is enabled only for the following types of lists / libraries.
- Document, picture, or form library
- Announcements list
- Calendar list
- Discussion board
- Blog
So here we have the challenge to receive incoming emails to our custom list. After doing some research we found that if we associate a SPEmailEventReceiver with my custom list, my list could also receive an email and “Incoming e-mail settings” will be available.
The following is the sample code where I have an EmailEventReceiver class derived from SPEmailEventReceiver and override the EmailReceived method.
- namespace MyNameSpace.EmailEventReceiver
- {
-
-
-
-
- public class EmailEventReceiver : SPEmailEventReceiver
- {
-
-
-
- public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)
- {
- }
- }
- }
In the preceding EmailReceived method, SPEmailMessage contains all the details like Subject, Body and so on. We can read the email subject and body as follows:
-
- string subject = emailMessage.Headers["Subject"];
-
-
- string htmlBody = emailMessage.HtmlBody;
-
-
- string htmlBody = emailMessage.PlainTextBody;
Since I have created a list using the object model, I have associated the preceding event receiver with my custom list as follows:
- if (list != null){
- list.EventReceivers.Add(SPEventReceiverType.EmailReceived, "my fully qualiefied assembly name", "MyNameSpace.EmailEventReceiver.EmailEventReceiver");
- list.Update();
- }
I hope this will help somebody and may save some time.
Thanks!