Friday, May 21, 2010

Receiving email to sharepoint list

Receiving an email to share point list is quite hard as compare to sending mail from list. This is one of the complicated thing on which I have worked in last few days. So here I am giving small solution for the problems that people are facing for this task, which I have seen on net while doing some goggling during task.The problems are like this type that list is enabled for receiving the mail but columns name not get updated ,how to debug the library..etc. Here i am giving some stuff may be people will get help from it.
I will explain as steps wise..
step1: For receiving mail to share point list you should have SMTP server install on your computer with its domain name properly configured
Step2: In Central Administration site in Operation section incoming mail settings needs to be done Here you will have to mention the name of domain server to which mail will be sent.
Step3: After done with all this the important factor is you will have to write the Email Receiver Handler library and register it to the specified share-point list. This event will fire whenever it receives mail and process as per the business logic you written in it.The DLL must need to add at GAC.
Step4: After registering the handler to list go to list settings.In communication tab there must be on link called In Coming Email settings. Click it and give the email address for this list.

Step5: Once done with above steps shoot a mail to specified library and check it out in C:\inetput\mailroot\drop folder . The mail comes first at here then Share point timer service is checking after one min duration for this folder.It transfer it to given list address.

Now I will give some code stuff for writing handler

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.EmailIntegration;
using System.Text.RegularExpressions;

namespace EmailReceiverHandlers
{
///
/// This class adds custom data from email to list
///

class MailEventHandler : SPEmailEventReceiver
{
SPListItem item;
public override void EmailReceived(SPList list, SPEmailMessage emailMessage, string receiverData)
{
try
{
list.ParentWeb.AllowUnsafeUpdates = true;
list.EnableAssignToEmail = true;
list.EnableAttachments = true;
item = list.Items.Add();
item["Title"] = emailMessage.Headers["subject"].ToString();
item.Update();
}
catch (Exception ex)
{
// EventLog.WriteEntry(sSource, "Error occured in EmailReceived method of class
MailEventHandler of type " + ex.Message, EventLogEntryType.Error);
}


}
}

Use OWSTIMER process from attach process for debugging the code.
Thanks..!

No comments: