Enabling Incoming Email Setting For Custom Generic List in SharePoint

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.
                              communication
But by default this option is enabled only for the following types of lists / libraries.

  1. Document, picture, or form library
  2. Announcements list
  3. Calendar list
  4. Discussion board
  5. 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.

  1. namespace MyNameSpace.EmailEventReceiver  
  2. {  
  3.   
  4.     /// <summary>  
  5.     /// List Email Events  
  6.     /// </summary>  
  7.     public class EmailEventReceiver : SPEmailEventReceiver  
  8.     {  
  9.        /// <summary>  
  10.        /// The list received an e-mail message.  
  11.        /// </summary>  
  12.        public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)  
  13.        {  
  14.     }  
  15.    }  

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:

  1. //Email subject  
  2. string subject = emailMessage.Headers["Subject"];  
  3.   
  4. //Email message in HTML format  
  5. string htmlBody = emailMessage.HtmlBody;  
  6.   
  7. //Email message in Plain text format  
  8. 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:

  1. if (list != null){  
  2.    list.EventReceivers.Add(SPEventReceiverType.EmailReceived, "my fully qualiefied assembly name""MyNameSpace.EmailEventReceiver.EmailEventReceiver");  
  3.    list.Update();  

I hope this will help somebody and may save some time.
Thanks!

Similar Articles