Send Mail in SharePoint

Leave a Comment
Hi All,

In a following post we will see how to send mail by using SMTP in SharePoint.
As a Prerequisites SMTP needs to be Configured in your Server and Out going mail is need to be set in Central Administration.


public bool SendMail(BAL objCommon,
                            SPWeb oWeb)
        {
            bool result = false;
            string From=string.Empty;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(objCommon)))
                    {
                       // Reading From Email ID from the Central Administration Settings.
                        var message = new System.Net.Mail.MailMessage
                        {
                            From = new System.Net.Mail.MailAddress(oWeb.Site.WebApplication.OutboundMailSenderAddress)
                        };
                       
                       // Reading To Emails from the Object and adding to the MailMessage                               object "message"
                        if (!string.IsNullOrEmpty(Convert.ToString(objCommon.ToMailAddress)))
                        {
                            string toAddress = Convert.ToString(objCommon.ToMailAddress).TrimEnd(',');
                            string[] toArray = toAddress.Split(',');
                            foreach (string t in toArray)
                                message.To.Add(new System.Net.Mail.MailAddress(t));
                        }
                       // Reading CC Emails from the Object and adding to the MailMessage                               object "message"
                        if (!string.IsNullOrEmpty(Convert.ToString(objCommon.Cc)))
                        {
                            string[] ccArray = Convert.ToString(objCommon.Cc).Split(',');
                            foreach (string t in ccArray)
                            {
                                if (!string.IsNullOrEmpty(t))
                                    message.CC.Add(new System.Net.Mail.MailAddress(t));
                            }
                        }
                        //Reading Subject and Assigning to the Object
                        message.Subject = Convert.ToString(objCommon.MailSubject);
                        // Reading Mail body and assigning to the Object
                        message.Body = Convert.ToString(objCommon.MailBody);
                        message.IsBodyHtml = true;
                        var smtpClient = new System.Net.Mail.SmtpClient(Convert.ToString(oWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address))
                        {
                            UseDefaultCredentials = true
                        };
                        smtpClient.Send(message);
                        message.Dispose();
                        result = true;
                    }
                });
                return result;
            }
            catch (Exception ex)
            {
                //Exception Handling
            }

        }

NOTE: Above code is compiled and tested in both SharePoint 2010 and SharePoint 2013 Environments.

Related Post

0 comments:

Post a Comment