SPUtility.SendEmail with StringBuilder and Powershell in SharePoint 2016

SPUtility.SendEmail with StringBuilder and Powershell
InfoPath alternatives for form designing SharePoint

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

Scenario:

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:

Hi Krishna,
Your loan has been approved and please find the below loan information.

Loan Application Number Applicant Name Loan Type Loan Amount Loan Interest (%) Loan Term
LN0007584 Krishna. Vandanapu House Loan $750,000 12.5% 60 Months

Thank you,
Loan Team

Below is the code to achieve the format:



SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite objSite = new SPSite(“http://cotesoApplication/”))
{
using (SPWeb spWeb = objSite.OpenWeb())
{
StringDictionary headers = new StringDictionary();
headers.Add(“from”, “conteso_noreply@conteso.com”);
headers.Add(“to”, “krishna.vandanapu@conteso.com”);
headers.Add(“bcc”, “krishna.vandanapu@conteso.com”);
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(“<tr>”);
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(“</tr>”);
strMessage.Append(“<tr>”);
strMessage.Append(“<td>LN0007584</td>”);
strMessage.Append(“<td>Krishna.Vandanapu</td>”);
strMessage.Append(“<td>House Loan</td>”);
strMessage.Append(“<td>$750,000</td>”);
strMessage.Append(“<td>12.5%</td>”);
strMessage.Append(“<td>60 Months</td>”);
strMessage.Append(“</tr>”);
strMessage.Append(“</table>”);
strMessage.Append(“<br/>”);
strMessage.Append(“Thank you,</br>Loan Team”);
strMessage.Append(“</div>”);
SPUtility.SendEmail(spWeb, headers, strMessage.ToString());
}
}
});

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



SPUtility.SendEmail with StringBuilder and Powershell
SPUtility.SendEmail with StringBuilder and Powershell

Now let us learn sending email using power shell and SPUtility.SendEmail
$site = New-Object Microsoft.SharePoint.SpSite(“http://sharepoint.company.com”)

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

$mail = [Microsoft.Sharepoint.Utilities.SpUtility]::SendEmail($web,0,0,”support@domain.com”,”Subject of the Mail”,”mail body”)
When we PowerShell SharePoint uses the from email address configured as part of outgoing email configuration in Central Admin.

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

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

Similar SharePoint 2013 Tutorials

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 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

View all posts by Krishna Vandanapu →

Leave a Reply