Microsoft & .NETHow To Send Email with SPUtility

How To Send Email with SPUtility content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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’s SendEmail method.

There are many scenarios where you might want to send an email notification. Figure 1 shows a sample scenario.

Figure 1. Email Notifications:
This illustration depicts one possible scenario where you might want to send an automatic email notification from SharePoint.

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’s SendEmail method 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 using SPUtililty.SendEmail:

private Boolean SendEmail()
    bool flag = false;
      using (SPSite site = new SPSite(
        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
          flag = SPUtility.SendEmail(web, true, true, 
                                     "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 the SendEmail method. Note that SendEmail occupies 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 are several overloads of the SendEmail method. The parameters for the version shown are an SPWeb object (web), followed by two Boolean parameters: AppendHtmlTags, which when true appends an HTML tag to the message, false otherwise, and HtmlEncode, which when true encodes the message and replace characters in HTML tags with entities. The last three parameters are strings that correspond to the email’s To, Subject, and Body fields, respectively.

The SendEmail method returns a Boolean true for success or false if the send attempt failed.

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

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

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories