Second Life of a Hungarian SharePoint Geek

October 9, 2012

Checking programmatically if an e-mail address is already in use for incoming mail

Filed under: Incoming email, Reflection, SP 2010 — Tags: , , — Peter Holpar @ 21:53

Recently I work quite a lot with the SharePoint calendar and the incoming mail feature of SharePoint. During my experiments I found an interesting problem, namely how one can check from code if a specific e-mail address is already configured for any of the lists.

Note: Since the domain-part of the address – that is the part after the @ sign – is fixed as part of the System Settings at the Central Administration, we can only set the e-mail alias – the part before the @ sign – for the lists.

clip_image001

Without this capability, the only option is the trial and error method that means we try to assign the mail address from code, and call the Update method of SPList as illustrated below:

SPList list = web.Lists[listName];
list.EmailAlias = "MailAlias"; // the part of the e-mail address before the @ sign
list.Update();

In the case the alias is already reserved for another list, we receive an SPException (Unable to assign this e-mail address to the list, because the address is in use.) that we could optionally handle using a try/catch block.

As you may know using this kind of error handling structure for checking existing items does not perform well, so if there is a great chance for conflicting mail addresses, it would be far better (and faster) to make the check without exceptions.

Let’s see what happens when we try to save the list configuration either from the UI or by custom code.

First, the Update(bool bFromMigration) method of the SPList is called, that calls the AssignAlias(string alias, SPList list) method of the SPEmailMap class. It validates the format of the alias via the static ValidateAlias(string alias) method, and then check the existence of a list having this alias through the CanGetListFromDatabase(string alias) method. This method uses another method GetEmailAliasRecordFromDatabase(string alias) that access the SharePoint database and returns list information in the form of an EmailAliasRecord object.

Note: the e-mail addresses used for this kind of check are stored in the EmailEnabledLists table (with fields Alias, Deleted, SiteId, WebId, ListId) of the configuration database SharePoint. The stored procedure used to get the list data based on the alias is the proc_getEmailEnabledListByAlias.

In the following code, I illustrate how to call the CanGetListFromDatabase method to check the existence of an alias.

Note: the code below uses non-public API calls and so it is not a supported approach. Use this sample at you own risk and preferably only in test environments.

  1. private bool IsAliasInUse(string mailAlias)
  2. {
  3.     bool result = false;
  4.  
  5.     string spEmailMapTypeName = "Microsoft.SharePoint.Administration.SPEmailMap";
  6.     // hack to get the Microsoft.SharPoint assembly
  7.     Assembly sharePointAssembly = typeof(SPWeb).Assembly;
  8.     // and a reference to the type of the SPEmailMap internal class
  9.     Type spEmailMapType = sharePointAssembly.GetType(spEmailMapTypeName);
  10.  
  11.  
  12.     // spEmailMap will be of type internal class
  13.     // Microsoft.SharePoint.Administration.SPEmailMap
  14.     // defined in Microsoft.SharePoint assembly
  15.     object spEmailMap = sharePointAssembly.CreateInstance(spEmailMapTypeName, false,
  16.         BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
  17.  
  18.     if (spEmailMap != null)
  19.     {
  20.         MethodInfo mi_CanGetListFromDatabase = spEmailMapType.GetMethod("CanGetListFromDatabase",
  21.                 BindingFlags.NonPublic | BindingFlags.Instance, null,
  22.                 new Type[] { typeof(string) }, null
  23.                 );
  24.         if (mi_CanGetListFromDatabase != null)
  25.         {
  26.             // result is bool
  27.             result = (bool)mi_CanGetListFromDatabase.Invoke(spEmailMap,
  28.                 new Object[] { mailAlias });
  29.  
  30.         }
  31.     }
  32.  
  33.     return result;
  34. }

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: