Wednesday, 8 May 2013

Enabling the incoming email setting for custom generic list

Hello All,

In our one of the SharePoint 2010 project, we have one of the requirement is our custom list should receive an email and message from email should be updated in one of the list column.

Incoming e-mail settings option is enable under Communications column in list settings. 

1.       Document, picture, or form library
2.       Announcements list
3.       Calendar list
4.       Discussion board
5.       Blog

So here we have challenge to receive incoming emails for our custom list. After doing some research it found that if we associate SPEmailEventReceiver to my custom list, my list could also receive an email and “Incoming e-mail settings” will be available.

Following is the sample code where I have EmailEventReceiver class derived from SPEmailEventReceiver and override the EmailReceived method.

namespace MyNameSpace.EmailEventReceiver

    /// <summary>
    /// List Email Events
    /// </summary>
    public class EmailEventReceiver : SPEmailEventReceiver
       /// <summary>
       /// The list received an e-mail message.
       /// </summary>
       public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)

In above EmailReceived method,  SPEmailMessage contains all the details like Subject, Body etc.  We can read email subject and body as follows

//Email subject
string subject = emailMessage.Headers["Subject"];

//Email message in HTML format
string htmlBody = emailMessage.HtmlBody;

//Email message in Plain text format
string htmlBody = emailMessage.PlainTextBody;

Since I have created list through object model, I have associated above event receiver to my custom list as follows:

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


Enjoy reading J

Feel free to comment / feedback if any or if you have any query J

1 comment:

Adapt software India said...

SharePoint Online delivers the powerful features of SharePoint without the associated overhead of managing the infrastructure on your own. Flexible management options ensure that you still retain the control you need to meet the compliance requirements of your organization. You can purchase SharePoint in the cloud as a standalone offering or as part of an Office 365 suite where you could also get access to Exchange, Lync, the Office clients and web apps.
Sharepoint developer
Sharepoint Site
Sharepoint Services
Sharepoint Designer
Sharepoint Consulting
Sharepoint Server