Programmatically create and configure Mail Enabled lists

Being able to mail enable a sharepoint list is pretty cool; once enabled an email can receive email, save attachments, etc.. But what’s the address of the lists? How do you enable it? How are attachments stored, and how do we decide who to let email it?

emailsettings

Well, for a customer we wanted to email enable a list with an address based on the site’s title. This meant that the site would have to be created before we could enable the list. So, I stapled a feature to the site’s definition, and used a feature receiver to run my code.

The initial information on how to email enable a list came from here (thanks Niels Loup), but it doesn’t describe how to set up the other settings. Well, here’s my code:

SPWeb web = properties.Feature.Parent as SPWeb;
SPList mailbox = web.Lists[MAILBOX];
if (mailbox.CanReceiveEmail)
{
mailbox.EnableAssignToEmail = true;
mailbox.EmailAlias = Regex.Replace(web.Title, @"[^0-9a-z]", "", RegexOptions.IgnoreCase);
SPFolder rootFolder = mailbox.RootFolder;
rootFolder.Properties["vti_emailusesecurity"] = 0;
rootFolder.Properties["vti_emailsaveattachments"] = 1;
rootFolder.Properties["vti_emailattachmentfolders"] = "subject";
rootFolder.Properties["vti_emailoverwrite"] = 0;
rootFolder.Properties["vti_emailsavemeetings"] = 0;
rootFolder.Properties["vti_emailsaveoriginal"] = 1;
rootFolder.Update();
mailbox.Update();
}
else
{
throw new SPException(string.Format("'{0}' can not receive mail", MAILBOX));
}

You can see we’re enabling the list, setting it’s email address based on the Site Title (minus any non-alphanumeric characters. In our solution the site title will be a unique reference number anyway, so that should be okay).

The tricky bit is the other settings – which are actually stored on the rootfolder of the list! Most of those settings are zero (0) or one (1) for on or off. The exception to this is the email attachments folders setting which can be “sender”, “subject” or “root”.

Advertisements
Programmatically create and configure Mail Enabled lists

5 thoughts on “Programmatically create and configure Mail Enabled lists

  1. Fernando says:

    I am doing exactly what you are showing using Windows PowerShell on a WSS install (no MOSS)
    Setting the RootFolder properties works fine.
    Setting the EmailAlias property works fine.

    Setting the EnableAssignToEmail executes but does not actually persist.
    The $Library.Update() line reports:

    Exception calling “Update” with “0” argument(s): “Error in the application.”
    $Library.Update( <<<< )

    If I eMail enable the document library manually through the interface it works fine and the email contact is created, etc. However, when I then look at the properties for the document library, the EnableAssignToEmail property is set to false eventhough it is actually enabled.

    I read somewhere that sharepoint keeps a list of email enabled document libraries. Maybe that is where it needs to be set?
    How might that be done?

    Thanks for you help.

  2. Sorry, I don’t know – I’ve never used Powershell. Certainly, the code above is what I’ve used, and it seems to work alright.

  3. Crespo says:

    Hi Andy, thank you for this post as it was very helpful and I’m sure by the time you wrote it it was working fine but with SP 2013 there are some things worth notice:

    – this property doesn’t exist anymore: rootFolder.Properties[“vti_emailsaveattachments”] = 1;
    – this property is not necessary to enable incoming email mailbox.EnableAssignToEmail = true;
    – this mailbox.Update() has to be called before rootFolder.Update() in order to ”
    Allow this document library to receive e-mail?” be set to True

    Again, many thanks for this post! 🙂

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.