Home > ASP.NET, C#, MOSS 2007, SharePoint > Enable Incoming emails on a Custom SharePoint List

Enable Incoming emails on a Custom SharePoint List


We had a requirement recently whereby we needed to create a custom SharePoint list that would accept incoming emails. This post is going to be very long so bear with me …

Initially, I was very optimisitic that all I needed to do was to setup incoming notifications via Central Administration and then turn on Incoming Email notifications on the list itself and it should all fall into place.

How wrong I was!

Setting up the Development Enviornment

The first thing you have to do is to setup your development enviornment so that you can send emails and your SharePoint list can recieve them. This turned out to be a massive task. I spent hours searching through blogs, forums and various sites to try and setup the infrastructure on my dev (Hyper-v VM based) enviornment but I just could not get it to work. Eventually I stubmled upon this post from Marc Charmois that explained in detail how to get the whole thing to work on your Dev enriornment:

Enabling Incoming-emails on the SharePoint List. The Problem:

Ok, so I created a custom SharePoint List went to List Settings and looked for the ‘In-coming email settings’ link under the ‘Communication’ settings and found it to be non-existent. Googling on this brought all sorts of theories, hearsay and rumours to light. Some thought that this option was only available on x,y or z type of lists then there were others that thought it was only available on a,b,c or d type of lists but one thing was for sure no one seemed to know for a fact what was actually going on. As I was looking at the object model I found a property on the SPList object called ‘CanReceieveEmail’. I thought maybe this was the answer i.e. all I needed to do was to get my list and set this property to true and it will all work. However you cannot set this property you can only get its value. I believe this property is used by the SharePoint UI to decide whether or not to show the ‘In-coming Email Settings’ link in the list settings area.

So it was time to fire up Reflector to find a way to set this property maybe through reflection?

Looking at the property via reflection I found:

public bool get_CanReceiveEmail()
{
    if (!SPEmailHandler.HasHandler(this.BaseTemplate) && !this.HasExternalEmailHandler)
    {
        return false;
    }
    return !SPMeeting.IsMeetingWorkspaceWeb(this.ParentWeb);
}

And ..

public static bool HasHandler(SPListTemplateType templateType)
{
    if ((((templateType != SPListTemplateType.Announcements) && (templateType !=    SPListTemplateType.Events)) && ((templateType != SPListTemplateType.DocumentLibrary)  && (templateType != SPListTemplateType.PictureLibrary))) && ((templateType !=  SPListTemplateType.XMLForm) && (templateType != SPListTemplateType.DiscussionBoard)))
    {
        return (templateType == SPListTemplateType.Posts);
    }
    return true;
}

And finally ….

internal bool HasExternalEmailHandler
{
    get
    {
        bool flag = false;
        foreach (SPEventReceiverDefinition definition in this.EventReceivers)
        {
            if (definition.Type == SPEventReceiverType.EmailReceived)
            {
                flag = true;
            }
        }
        return flag;
    }
}

From this we can determine the following:

If the list, regardless of its type, appears in a meeting workspace web it will not be able to recieve in-coming emails.

The list either needs to have the BaseTemplate of one of the following:

  • Announcements
  • Events
  • DocumentLibrary
  • PictureLibrary
  • XMLForm
  • DiscussionBoard
  • Posts

or it needs to have an event handler of type ‘EmailReceived’ attached to it.

An important point to note here is that it mentions the base template which should not be mistaken for the BaseType. What this means is that you could create a custom list that inherits from the BaseType Document Library but that does not mean that it will have incoming emails enabled. Your list will need to have the same BaseTemplate as the out of the box lists mentioned above which is not really ideal.

Enabling Incoming-emails on the SharePoint List. The Solution:

So the only route left for me to take was to go the Event Handler path. I attached the Event Handler (and the incomming email settings link started to appear in the list settings area) then sent an email to my list and debugged my code with a break point on my event handler. However it just never seemed to get hit and neither were my emails appearing in the list itself.

The mistake I was making was to attach the w3wp process but this is not the process that processes the incoming emails. I dont want to go into this in detail but the incoming emails are processed by a SharePoint job that runs every minute therefore the process I needed to attach was the OWSTimer process. Once I attached this process it started to hit my break point however the emails were still not appearing in the list.

One thing to note here is that whenever you make any code changes to this event handler, after you deploy the dll’s to GAC, you need to ensure you restart the SharePoint Timer Job Service because it holds a cached version of the dll’s.

Finally, the reason the emails were not appearing in the list was because this needs to be done via the Event Handler. For the lists that are from one of the Templates I mentioned above SharePoint understands how to process and add the email messages however for your own custom list it is down to the Developer to write the code to do this processing. The event handler though provides you an object of type SPEmailMessage which has all the data you require. Below is an example of how it can be used to add the email subject to a simple custom list with only a title field:

public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)
{
    SPListItem newItem = list.Items.Add();
    newItem["Title"] = emailMessage.Headers["subject"]
    newItem.Update();
}

You can easily extend it to deal with attachments as well but that is for another day!

P.S Illustrations to be added soon….

Advertisements
  1. Steve
    November 21, 2010 at 12:02 pm

    Such a useful post, thank you.

    Having managed to implement this I’ve come across an interesting problem – I’m saving any email attachments as seperate SPFile objects into the document library, but I can’t see how to then remove the attachments from the SPEmailMessage object.

    Any ideas would be appeciated.

  2. jasear
    November 22, 2010 at 10:22 am

    Hi Steve,

    I havent played around with the attachments so much but my first question would be why do you want to remove it from the SPEmailMessage object?

  3. Steve
    November 23, 2010 at 9:18 am

    Firstly, the default behaviour of a document library is to do this – attachments are saved outside of the original email. This is presumably because you’re likely to want to treat the attachments as they’re own entity, e.g. Invoices etc.

    As I’m working with a custom list based on a document library, this has to be implemented manually in the EmailReceived event.

    Looked at creating a new SPEmailMessage but can’t see how to remove the attachments from the Stream.

  4. David
    October 10, 2011 at 1:31 am

    We use Cloud2050.com’s (http://www.cloud2050.com) email solution to sync emails to SharePoint list and document library. It doesn’t require server side configuration and it supports SharePoint in Office 365.

  5. david
    October 5, 2012 at 4:01 am

    what file is modified with the detail shown in your post?

    • jasear
      October 5, 2012 at 10:36 am

      Hi David,

      I am assuming you are referring to the EmailReceived method which is a custom event handler you have to create and attach to your custom SharePoint List.

  6. david2
    January 8, 2013 at 10:12 am

    A useful post. An inefficient alternative if you can’t butcher the accepted list templates to your needs would be to create a dummy list, and use conditional workflows to copy items / attachments to the required destination.

    Use either the workflow or retention to delete the dummy objects and avoid long term duplication.

    http://blogs.salmanghani.info/copy-item-workflow-using-sharepoint-designer-2010/

  7. March 2, 2013 at 6:54 am

    Hello, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam comments?
    If so how do you reduce it, any plugin or anything you
    can suggest? I get so much lately it’s driving me mad so any support is very much appreciated.

  8. Johnb32
    April 29, 2014 at 9:22 am

    I like this site its a master peace ! Glad I observed this on google. The definition of a beautiful woman is one who loves me. by Sloan Wilson. efgbgcfcgddd

  9. October 11, 2014 at 4:00 pm

    Quality articles or reviews is the important to interest the viewers to visit the website,
    that’s what this site is providing.

  10. Khushi Shaikh
    June 8, 2016 at 8:17 pm

    Can I update certain column of the list based on the Incoming email’s subject?

  1. November 11, 2010 at 1:35 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: