Blog Stats
  • Posts - 40
  • Articles - 0
  • Comments - 28
  • Trackbacks - 0

 

Programatically Sending Mail in SharePoint

Hi All,

There are two apporaches  to programatically Send Mail in SharePoint .

1. Using SPSecurity.SendMail

2. Using Classical ASP.NET (SMTPClient)

Note: Make Sure that Mail Server is Configured Properly in Centeral Administration.

Below is the code:

1. Using SPSecurity.SendMail

 public static bool SendMail(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
            {
                bool IsMailSent = false;
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                        {
                            using (SPWeb currentWeb = currentSite.OpenWeb(SPContext.Current.Web.ID))
                            {

                                 IsMailSent = SPUtility.SendEmail(currentWeb, true, true, To, Subject, Body);
                                 /* 
                                  * If you want to send HTML Content then use this method
                                  * IsMailSent = SPUtility.SendEmail(currentWeb, true, false, To, Subject, Body);
                                  */
                                 IsMailSent = true;

                            
}
                        }
                    });
                    return IsMailSent;
                }
                catch (Exception ex)
                {
                    IsMailSent = false;
                    return IsMailSent;
                }
            }

Disadvantage:

1. Attachments are not allowed.
2. The message body cannot exceed 2048 characters in Single Line. The Workaroung is Add NewLine ("\n") characters to keep lines under 2048 char.

2. Using Classical ASP.NET (SMTPClient)


public static bool SendMail(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
            {
                bool IsMailSent = false;
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                        {
                            using (SPWeb currentWeb = currentSite.OpenWeb(SPContext.Current.Web.ID))
                            {
                                From = currentWeb.Site.WebApplication.OutboundMailReplyToAddress.ToString();

                                using (MailMessage mailMessage = new MailMessage(From, To, Subject, Body))
                                {
                                    mailMessage.IsBodyHtml = IsBodyHtml;
                                    SmtpClient smtpClient = new SmtpClient();
                                    smtpClient.Host = currentWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

                                    using (smtpClient as IDisposable)
                                    {
                                        smtpClient.Send(mailMessage);
                                    }
                                }

                                IsMailSent = true;
                            }
                        }
                    });
                    return IsMailSent;
                }
                catch (Exception ex)
                {
                    IsMailSent = false;
                    return IsMailSent;
                }
            }

 

 

Thanks- Jayant Sharma


Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

 

 

Copyright © Jayant Kumar