Tuesday, January 27, 2009

Receiving / enabling emails in the list or library

Hello All,

Today I am going to discuss something about the receiving emails in the list. Before that let me discuss some of the points.

You can receive the emails in one of the following lists.
(1) Document Library
(2) Announcement
(3) Calendar
(4) Discussion
(5) Blog

But before enabling email support in the list, you need to enable incoming email support in the central administration. Under operations->Incoming mail settings.

We will go for enabling for document library.

So first go and check for the incoming email options in central administration. Then only you can see incoming mail settings options under communication section for the document library.

Go to the incoming email settings.

Select allow this document library to receive emails.

Specify the email address for this document library. If anyone sends mail to this email address, mail will be coming down to this document library.

Keep save all attachments in rootfolder and at last keep Accept e-mail messages from any sender options selected.

I also want to give more highlight on one important email handler class. Once you have the document library enabled with the email receiver, then there is one class which you can use and override its method.

The class is SPEmailEventReceiver and you need to override its EmailReceived method. When email is received by the list and event handler is attached to the list. Here is a code which extracts the subject and body from the email and adds the item to the list.

public class EmailHandler: SPEmailEventReceiver
{
public override void EmailReceived(
SPList objList,
SPEmailMessage objMessage,
string strReceiverData)
{
SPListItem objListItem = objList.Items.Add();
objListItem["Title"] = objMessage.Headers["Subject"];
objListItem["Body"] = objMessage.HtmlBody;
objListItem.Update();
}
}


That’s it. Enjoy email receiving.

5 comments:

Anonymous said...

Kings,

I have not been able to set this up correctly. I keep getting this error in the error log:

Event manager error: Could not load type 'SPFeedBack.SPFeedback_Handler' from assembly 'SPFeedbackWSP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0ba07e60a9eec119'.

please:
kristopher.baker@dot.state.fl.us


code:
public class EmailReceived_Handler : SPEmailEventReceiver
{
public override void EmailReceived(
SPList objList,
SPEmailMessage objMessage,
string strReceiverData)
{
base.EmailReceived(objList, objMessage, strReceiverData);

SPListItem objListItem = objList.Items.Add();
objListItem["Title"] = objMessage.Headers["Subject"];
objListItem["Body"] = objMessage.PlainTextBody;
objListItem.Update();
}
}

Anonymous said...

Thank you for your provided solution!
I'm new to SharePoint development. How can I find/access the "EmailReceived_Handler : SPEmailEventReceiver" class?

Thank you for your help!

SharePoint Kings said...

@Anonymous - 1,
you have to create put DLL in GAC and add safe control


@Anonymous - 2,
you will not find this class you have to create it.

Anjan kumar Maity said...

I have used SPEmailEventReceiver one of my project. It was working fine. Now a days the event is not firing automatically. I have to restart share point timer regularly. Then it picks the .eml files from inetpub/../drop folders. and its start works.
Any Idea ?How to solve this permanetly?

SharePoint Kings said...

Anjan kumar Maity,
did you get any log entry in SharePoint log or Windows Application Log.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com