Send an email using SharePoint Server Object Model

In this SharePoint tutorial, we will discuss how to Send an email using SharePoint Server Object Model.

As we know there are many ways to send an email to user after firing any events in the SharePoint application. We can also use the Event receiver and we can also use workflow and we can also use simple custom coding to send an email to the user.

Send an email using SharePoint Server Object Model

Here, we will see how to send an email using SharePoint Server Object Model in SharePoint 2013/2016.

Step 1: Create a SharePoint List and create a field called Email which you take as a reference to send an email to the user.

Send an email using SharePoint Server Object Model

Step 2: Next create a visual web part to add an item into a SharePoint List. So here I am using the SharePoint server object model to add an item to a list. Once Item successfully inserted into the list, the system will trigger an email to the user.

Step 3: Here I am adding a code to send an email to user using code.

Note: Make sure you have already configured the outgoing email service in your SharePoint server.

Send an email programmatically in SharePoint
 MYDictionary emailContext = new MYDictionary();
 emailContext.Add(ContextKeys.MailTo, this.txtEmail.Text.Trim());
 emailContext.Add(ContextKeys.Subject, string.Format((string)GetLocalResourceObject("subject")));
emailContext.Add(ContextKeys.Body, string.Format((string)GetLocalResourceObject("RequestEmailFormat")));
SendEmail(emailContext);                       

Here I am using LocalResourceobject to keep my text in different languages. So if you have only a single language the no need to add Local Resource file here. Just add your text here instead of a local resource.

Step 4: Next call SendEmail function to send an email to user.

public override bool SendEmail(Dictionary<string, string> context)
        {
            bool flag = false;

            try
            {

                string sendTo = String.Empty;
                string sendSubject = String.Empty;
                string body = String.Empty;

                context.TryGetValue(ContextKeys.Subject, out sendSubject);
                context.TryGetValue(ContextKeys.MailTo, out sendTo);
                context.TryGetValue(ContextKeys.Body, out body);
                body = HttpUtility.HtmlDecode(body);
                StringDictionary headers = new StringDictionary();
                headers.Add("to", sendTo);
                                              
                    headers.Add("bcc", "rkiran44@hotmail.com");                                                              
                    headers.Add("from", "<no-reply@pikasha.in>");
                    headers.Add("subject", sendSubject);
                    headers.Add("content-type", "text/html");

                    SPSecurity.RunWithElevatedPrivileges(delegate ()
                    {
                        using (SPSite site = new SPSite(
                          SPContext.Current.Site.ID,
                          SPContext.Current.Site.Zone))
                        {
                            using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                            {
                                
                                flag = SPUtility.SendEmail(web, headers,
                                                               body);
                            }
                        }
                    });
                }
            
            catch (Exception ex)
            {
                CommonData.ProcessDataException(ex);
                flag = false;
            }

            return flag;
        }

Step 5: Next go your web part and add an item to a list. So after insert coding, you just call this SendEmail method to send an email.

Send an email using SharePoint Server 2019

You may like following SharePoint tutorials:

In this SharePoint tutorial, we learned how to send an email using SharePoint Server Object Model.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>