Send email programmatically in SharePoint 2016/2013

This SharePoint tutorial, we will discuss how to send email programmatically using the server object model in SharePoint 2013/2016. I will also show how to send email in SharePoint 2016 using PowerShell.

Send email programmatically in SharePoint 2016/2013

In this article, we will learn how to leverage StringBuilder and Powershell to send a minimal HTML body email using SPUtility.SendEmail.

We have to send an email to loan users with the details of their loan approval and pending information. In this email, we want to mark the loan depart team also.

Expected email:

send email programmatically in SharePoint
using (SPSite objSite = new SPSite("http://cotesoApplication/"))
using (SPWeb spWeb = objSite.OpenWeb())
StringDictionary headers = new StringDictionary();
headers.Add("from", "");
headers.Add("to", "");
headers.Add("bcc", "");
headers.Add("subject", "Krishna – Your Loan Details");
headers.Add("fAppendHtmlTag", "True"); //To enable HTML format
System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
strMessage.Append("<div style='font-family:Calibri;'>");
strMessage.Append("Hi Krishna,<br/>");
strMessage.Append("Your loan has been approved and please find the below loan information.<br/><br/>");
strMessage.Append("<table border='2' style='font-family:Calibri; font-size:smaller;border-collapse:collapse;border-color:rgb(195,195,195)'>");
strMessage.Append("<td style='font-weight:bold'>Loan Application Number</td>");
strMessage.Append("<td style='font-weight:bold'>Applicant Name</td>");
strMessage.Append("<td style='font-weight:bold'>Loan Type</td>");
strMessage.Append("<td style='font-weight:bold'>Loan Amount</td>");
strMessage.Append("<td style='font-weight:bold'>Loan Interest(%)</td>");
strMessage.Append("<td style='font-weight:bold'>Loan Term</td>");
strMessage.Append("<td>House Loan</td>");
strMessage.Append("<td>60 Months</td>");
strMessage.Append("Thank you,</br>Loan Team");
SPUtility.SendEmail(spWeb, headers, strMessage.ToString());


  • No support for attachments
  • Message body should not exceed 2048 characters.

This is how the email looks like:

send email programmatically in SharePoint 2016

Send email using PowerShell in SharePoint 2016/2013

Now let us learn to send emails using PowerShell and SPUtility.SendEmail in SharePoint 2013/2016.

$site = New-Object Microsoft.SharePoint.SpSite("")

#We can use: Get-SPWeb in SharePoint 2010/2013/2016
$web = $site.OpenWeb()

$mail = [Microsoft.Sharepoint.Utilities.SpUtility]::SendEmail($web,0,0,"","Subject of the Mail","mail body")

When PowerShell SharePoint uses the from email address configured as part of an outgoing email configuration in SharePoint Central Admin.

Central Administration -> System Settings -> Configure outgoing e-mail settings

You should be able to see the from email-id as part of the “Outbound SMTP Server”.

HTML Email body getting corrupted with SPUtility.SendEmail

Now, we will discuss an issue HTML Email body getting corrupted while sending an email with SPUtility.SendEmail”. The email body formatting issue.

Below is how the Email body is appearing.

HTML Email body getting corrupted with SPUtility.SendEmail

And below is how the email body is expected.

HTML Email body getting corrupted with SPUtility.SendEmail

I was trying to send email to project owner as soon as user complete the task with the below syntax:

SPUtility.SendEmail(SPContext.Current.Web, false, false, strEmailTo, strMailSubject , strMailBody);

Where strMailBody:

<div style='font-family:Calibri;'>Hi Project Owner,<br />
<br /> The Task Owner – Krishna has submitted task vales successfully. Please review as per your availability<br /><br />
<table><tr><td style='font-weight:bold'>Tasks for the Month: </td><td>201710</td>
<tr><td style='font-weight:bold'>Report Type: </td><td>Rule Level</td></tr>
<tr><td style='font-weight:bold'>Project Type: </td><td>Non-SFO</td></tr></table>
</br>Completed Task Details: <br />
<table border='2′ style='font-family:Calibri; font-size:smaller;border-collapse:collapse;border-color:rgb(195,195,195)'>
<tr style='font-weight:bold'><td>RuleOwner</td><td>TotalRecordsAssigned</td><td>InprogressRecords</td><td>DraftRecords</td><td>CompletedRecords</td><td>Status</td></tr>
<tr style='background-color:#52778d;color:white'><td>Glenn Jones</td><td>38</td><td>0</td><td>0</td><td>38</td><td>Completed</td></tr>
<tr><td>Task Owner 1</td><td>65</td><td>0</td><td>0</td><td>65</td><td>Completed</td></tr>
<tr><td>Task Owner 2</td><td>74</td><td>0</td><td>0</td><td>74</td><td>Completed</td></tr>
<tr><td>Task Owner 3</td><td>59</td><td>0</td><td>0</td><td>59</td><td>Completed</td></tr>
<tr><td>Task Owner 4</td><td>49</td><td>0</td><td>0</td><td>49</td><td>Completed</td></tr>
<tr><td>Task Owner 5</td><td>116</td><td>0</td><td>0</td><td>116</td><td>Completed</td></tr>
<tr><td>Task Owner 6</td><td>32</td><td>0</td><td>0</td><td>32</td><td>Completed</td></tr>
<tr><td>Task Owner 7</td><td>14</td><td>0</td><td>0</td><td>14</td><td>Completed</td></tr></table>
<br />InProgress Task Owners: <br />
<table border='2′ style='font-family:Calibri; font-size:smaller;border-collapse:collapse;border-color:rgb(195,195,195)'>
<tr style='font-weight:bold'><td>RuleOwner</td><td>TotalRecordsAssigned</td><td>InprogressRecords</td><td>DraftRecords</td><td>CompletedRecords</td><td>Status</td></tr>
<tr><td>Task Owner 11</td><td>155</td><td>155</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 12</td><td>78</td><td>0</td><td>1</td><td>77</td><td>Inprogress</td></tr>
<tr><td>Task Owner 13</td><td>16</td><td>16</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 14</td><td>64</td><td>64</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 15</td><td>108</td><td>108</td><td>0</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 16</td><td>60</td><td>59</td><td>1</td><td>0</td><td>Inprogress</td></tr>
<tr><td>Task Owner 17</td><td>158</td><td>136</td><td>22</td><td>0</td><td>Inprogress</td></tr></table>
<br /> Thank you,</br>GS Team</div>

If we notice the SPUtility.SendEmail it supports only string for Email body parameter. String variable limitation is only 509 characters (MSDN Article of String limitations).

Since the String limitation is only 509 characters so it gets corrupt.

We have to use the classic .net c# SMTP code/syntax to send the email with this kind of email body (more than 509 characters length).

Below is the code snippet for the same.

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.Subject = strMailSubject;
message.From = new System.Net.Mail.MailAddress("");
message.Body = strMessage.ToString();
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(<<SMTP Server configured in Central admin>>);

You may like following the SharePoint server object model:

This tutorial helps us to learn how to send an email programmatically using PowerShell and server object model in SharePoint 2013/2016.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)


Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019