How to programmatically send an email in SharePoint

Send Email programmatically in SharePoint Using Customize Email Body

Create a New List is SharePoint 



SendEmail.cs  - Get The Email Body According to the Email Title

 public class SendEmail
    {
        string spSitestring = SPContext.Current.Site.Url;
        Logger log = new Logger();

     public EmailBodyEntity getEmailBodyLinkTitle(string title)
        {
            try
            {
                using (SPSite spSite = new SPSite(spSitestring))
                {
                    spSite.AllowUnsafeUpdates = true;
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        spWeb.AllowUnsafeUpdates = true;
                        EmailBodyEntity emailBodyEntity = new EmailBodyEntity();
                        SPList list = spWeb.Lists.TryGetList("Your Email Body List");
                        SPQuery qryEmp = new SPQuery();

                        qryEmp.Query = "<Where><Eq><FieldRef Name='LinkTitle' /><Value Type='Text'>" + title + "</Value></Eq></Where>";

                        SPListItemCollection oSpListCln = list.GetItems(qryEmp);

                        foreach (SPListItem item in oSpListCln)
                        {
                            emailBodyEntity = new EmailBodyEntity();

                            emailBodyEntity.ID = Convert.ToInt32(item["ID"]);
                            if (item["LinkTitle"] != null)
                                emailBodyEntity.LinkTitle = item["LinkTitle"].ToString();
                            if (item["EmailBody"] != null)
                                emailBodyEntity.EmailBody = item["EmailBody"].ToString();
                            if (item["Subject"] != null)
                                emailBodyEntity.Subject = item["Subject"].ToString();
                        }
                        spWeb.AllowUnsafeUpdates = false;
                        return emailBodyEntity;
                    }
                }
            }
            catch (Exception ex)
            {
                log.EventLogWriter("System : TestSystem|| Method : getEmailBodyLinkTitle|| ERROR : " + ex.Message);
                throw;
            }
        }



public void SendEmail()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPWeb oSPWeb = SPContext.Current.Site.OpenWeb())
                    {
                        StringDictionary headers = new StringDictionary();
                        string mbody = "";
                        string emailTo = "";

                        EmailBodyEntity emailBodyEntity = getEmailBodyLinkTitle("Your Email Title");

                            if (emailBodyEntity != null)
                            {
                                mbody = emailBodyEntity.EmailBody;
                             
                                mbody = mbody.Replace("@RequestNumber", "SCH-1111");  // Replaces the Email body value
                                mbody = mbody.Replace("@Originator", "Shasshika");
                                mbody = mbody.Replace("@TodayDate", 2015.01.31);

                                headers.Add("from", "test@abc.com");
                                headers.Add("to", "test2@abc.com");
                                headers.Add("subject", emailBodyEntity.Subject);
                                headers.Add("fAppendHtmlTag", "True");  //To enable HTML format

                                System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
                                strMessage.Append(mbody);

                                SPUtility.SendEmail(oSPWeb, headers, strMessage.ToString());
                            }
                    }

                });
            }
            catch (Exception ex)
            {
                log.EventLogWriter("System : TestSystem || Method :
SendEmail|| ERROR : " + ex.Message);
                throw;
            }
        }

     }

---------------------------------------------Regards Shashika -------------------------------------------------

Comments

Popular posts from this blog

[SOLVED] The SharePoint Timer Service service terminated unexpectedly. Event ID : 7031

FIX : The password supplied with the username Domain\Username was not correct. Verify that it was entered correctly and try again.

FIX : stsadm is not recognized as an internal command