Creating an email receiver is not an over-documented area of SharePoint development, although it may be very useful when one needs to provide a custom collaboration solution or mail integration, so I decided to share some of my experiences on this field. Before creating the receiver, it is important to configure the incoming mail feature on your system. There are several excellent documents on the web that describe how to set up SharePoint 2007 for incoming mails. I usually use the following white papers to configure this feature:
The class of the event receiver should be derived from the SPEmailEventReceiver class and override the EmailReceived method. This method has three parameters. The first parameter is an SPList, it is the list instance the event receiver is registered for. The second parameter is an SPEmailMessage, it contains the data related to the incoming email, and is the most important parameter for us. The last parameter is a String, that is the value of the Data property specified for the event receiver in the related SPEventReceiverDefinition instance.
The Headers property of the SPEmailMessage contains several interesting mail properties. This collection is populated based on the headers of the raw message. A few of the headers are directly mapped to SPEmailMessage properties, like the Sender property is mapped to the From header.
If you have to know what headers the message has you can test that by including the following code snippet in the receiver method. Don’t forget to include a using directive for the System.Diagnostics namespace. You should know that the types of the headers may vary by the mail type, for example, an invitation may contain extra headers.
We will test our receiver on a standard document library. Let’s create the From, To, Subject, Body and Sent fields as illustrated on the following picture.
In the code, we will get the binary content of the mail first and store it into the list. We create the file name based on the mail subject, overwriting possible former files having the same name. It means that if you expecting multiple mails with the same subject – that is very likely – you should extend this logic a bit, for example, by including the date of the mail or some kind of numbering.
Next, we set the From, To, Subject, Body and Sent properties for the list item using the mail properties.
The following step is to build the project and to register the receiver. If you need info about the registration, you can find several methods of that on Brian Wilson’s blog:
Personally, I use the Event Handler Explorer most of the time.
Let’s test the receiver by sending a few mails to the mail address specified for the document library.
Be sure that the Windows SharePoint Services Timer service (OWSTIMER.EXE) is running. Also, if you create and deploy a new version of the receiver, restart the service, and attach to this process if you need to debug the receiver.
The picture below illustrates the document library after sending a few mails.