How To Send Email with SPUtility in Sharepoint

There is no doubt that email is a key functionality in today's enterprise. In organizations where SharePoint 2007 has also become a key collaboration platform you'll increasingly find situations where you need to send email from within the SharePoint environment. This article shows how to accomplish that, using the SPUtility class'sSendEmailmethod.

There are many scenarios where you might want to send an email notification.

In this scenario, whenever a document gets uploaded to the document library, the system first applies some business rules and logic, and then notifies users interested in that document by email. Although this is a simple scenario, it shows clearly how email is an important component of today's organizational strategies. As a SharePoint developer, that means you need a quick and easy way to send email.

Traditionally developers have used the SMTP class in ASP.NET to send email; it's quite powerful and provides fine granular control over the contents. But to use it, you need to add appropriate references and set the various properties. When your requirements are simple—such as simply sending some brief text notifications, or when email is only part of some other automated process or workflow, you may want to look at the SPUtility class'sSendEmailmethod instead. While it doesn't have the power of ASP.NET's SmtpClient class, it's much easier to use. The following example sends email usingSPUtililty.SendEmail:

private Boolean SendEmail() {   try   {     bool flag = false;     SPSecurity.RunWithElevatedPrivileges(       delegate()     {       using (SPSite site = new SPSite(         SPContext.Current.Site.ID,         SPContext.Current.Site.Zone))       {         using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))         {           flag = SPUtility.SendEmail(web, true, true,                                       "",                                      "Subject",                                      "This is a sample email Body");         }       }     });     return flag;   }   catch (System.Exception exp)   {     // Do some error logging     return false;   } }  

The first thing to remember is that you need to use the Microsoft.SharePoint.Utilities namespace, which contains the SPUtility class that exposes theSendEmailmethod. Note thatSendEmailoccupies only one line in the preceding code. You should also note that you may need to elevate privileges, because the current user context may not have sufficient permission to send email.

There areseveral overloads of theSendEmailmethod. The parameters for the version shown are an SPWeb object (web), followed by two Boolean parameters:AppendHtmlTags, which whentrueappends an HTML tag to the message,falseotherwise, andHtmlEncode, which whentrueencodes the message and replace characters in HTML tags with entities. The last three parameters are strings that correspond to the email'sTo,Subject, andBodyfields, respectively.

TheSendEmailmethod returns a Booleantruefor success orfalseif the send attempt failed.

As you can see, sending basic email usingSendEMailis straightforward; however, note these key considerations:

  • Attachments are not allowed.
  • By default, the message body cannot exceed 2048 characters.SendEMailwill truncate all characters beyond 2048. The workaround is to ensure that nosingle linein your message body exceeds 2048 characters. Simply add newline characters as needed to keep the lines under the 2048 character limit.
  • By default, theFromaddress is always set to the value from the Central Administration Outbound Mail Sender Address field.