Search This Blog

Thursday, May 12, 2016

How to send E-Mail using SPUtility.SendEmail


SharePoint SPUtility class has SendEmail method which is used to send email to any email address. By default, SPUtility.SendEmail() method picks the 'From address' from Outgoing E-Mail Settings in Central administration. Use SPUtility.IsEmailServerSet method to check if server is configured with SMTP mail settings.

Here is a C# Example for sending E-mail using SPUtility.SendEmail:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;
 
namespace HideUserInfo
{
    class SendEMail
    {
        static void Main(string[] args)
        {
 
            using (SPSite oSPSite = new SPSite("https://sharepoint.com"))  //Site collection URL
            {
                using (SPWeb oSPWeb = oSPSite.OpenWeb("News"))  //Subsite URL
                {
                    StringDictionary headers = new StringDictionary();
 
                    headers.Add("from""sender@domain.com");
                    headers.Add("to""receiver@domain.com");
                    headers..add("bcc","SharePointAdmin@domain.com");
                    headers.Add("subject""Welcome to the SharePoint");
                    headers.Add("fAppendHtmlTag","True"); //To enable HTML format
 
                    System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
                    strMessage.Append("Message from CEO:");
                    
                    strMessage.Append("<span style='color:red;'> Make sure you have completed the survey! </span>");
                    SPUtility.SendEmail(oSPWeb, headers, strMessage.ToString());
 
                }
            }
        }
    }
}



No comments:

Post a Comment