Programatically Send E-mail in Sharepoint 2010

In this post I will discuss various methods of Sending an e-mail in SharePoint 2010.

Method 1 – Code retrieves the SMTP host’s address from the SharePoint configuration database.

public static bool SendMail(string Subject, string Body,bool IsBodyHtml, string From, string To, string Cc, string Bcc)
bool mailSent= false;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = SPContext.Current.Site.WebApplication.
MailMessage mailMessage = new MailMessage(From, To, Subject, Body);
if (!String.IsNullOrEmpty(Cc))
MailAddress CCAddress = new MailAddress(Cc);
if (!String.IsNullOrEmpty(Bcc))
MailAddress BCCAddress = new MailAddress(Bcc);
mailMessage.IsBodyHtml = IsBodyHtml;
mailSent = true;
catch (Exception) { return mailSent; }
return mailSent;

This ensures that you get the advantages of.NET-style mail delivery while keeping your configuration in Central Administration.
If however, you are not working in a SharePoint context, you can get the SPWebApplication reference from a new SPSite object like below

public static string GetSharePointMailService(string mysite)
string address;
using (SPSite site = new SPSite(mysite))
address = site.WebApplication.OutboundMailServiceInstance.Server.Address;
return address;

Method 2 : – This uses the embedded SharePoint SendEmail which has fewer capabilities, but is as straightforward as possible, and is the preferred approach if you simply want to send e-mail:

SPUtility.SendEmail(web, useHtml, htmlEncode, to, subject, htmlBody)

The Paramets are :
web(SPWeb) – An object that represents the site
headers(StringDictionary) – A collection of additional headers
useHtml(bool) – Used to append an HTML tag to the message (true)
htmlEncode(bool) – Encodes the message and replaces characters in HTML tags with entities
to(string) – The address to which to send the e-mail
subject(string) – Contains the subject for the e-mail message
htmlBody(string) – Contains the body of the e-mail message
addFooter(bool) – Used if there is a footer to be appended to the e-mail

The headers parameter is used instead of the to and subject parameters. It allows you to set all possible mail headers and forces you to add at least the to header that way. An example follows:

StringDictionary headers = new StringDictionary();
headers.add(“subject”,”Send an EMail from SPUtility”);
string bodyText =”This is an html formatted message.”;
SPUtility.SendEmail(web, headers, bodyText);
Note that StringDictionary is defined in the System.Collections.Specialized namespace.

Method 3 : - Sending E-Mail from a WCF Service

Sending an e-mail from a service when SPContext is not available could fail. As a workaround, you have to prevent the mail function from reading the current context by using HttpContext.Current = null. If it can’t, it will retrieve the right context and it will then work. The next example shows this.

Sending E-mail Using SendMail from a WCF Service
using (SPSite site = new SPSite(“http://sharepointserve”))
SPWeb thisWeb = site.RootWeb;
string to = “”;
string subject = “Book Message”;
string body = “A message from SharePoint”;
HttpContext curCtx = HttpContext.Current;
HttpContext.Current = null;
bool success = SPUtility.SendEmail(thisWeb, true, true, to, subject, body);
HttpContext.Current = curCtx;
catch (Exception ex)
{// Exception handling skipped for clarity}

In the above code the current context is set to null to force the context to be retrieved again. Saving the current context ensures that the service works properly after the method has been executed.


  1. [...] the current context ensures that the service works properly after the method has been executed LikeBe the first to like this [...]

  2. [...] the current context ensures that the service works properly after the method has been executed LikeBe the first to like this [...]

  3. [...] Learning Sharepoint This entry was posted in Microsoft, Sharepoint and tagged Host, How to send email in sharepoint, [...]

  4. [...] Programatically Send E-mail in Sharepoint 2010 : Learning SharePoint [...]

Speak Your Mind


Disclaimer: The contents of this site are provided “AS IS”. Information and views expressed including URL(s) and other Internet website references, may change without notice. You bear the risk of using it. SharePoint, MOSS, SharePoint 2010, SharePoint 2013 and Microsoft are trademarks of the Microsoft Corporation.Learning SharePoint only provides Tips,Tricks and Tutorials for using Microsoft SharePoint, Microsoft Office and related Softwares.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.