Monday, January 28, 2013

Send email in Sharepoint - Explore Options!

Today I am going to explore options we have to send emails in SharePoint. I find it very interesting that there are a couple of ways to send emails. Sending notifications is a very frequently asked feature from the clients in any application and SharePoint has an out of box feature called alerts.

MSDN - Alerts Details



Option 1:

Store the SMTP server address in the web.config and can utilize the send email function as below:

This ensures that you get the advantages of.NET-style mail delivery while keeping your configuration at one place in your Central Administration.
 
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using Microsoft.SharePoint;

namespace Send_Email.Send_Email
{
    public partial class Send_EmailUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool sentemail = SendMail("Hello", "Test Body", false, "pallavi.sharma@xyz.com", "pallavi.sharma@neudesic.com", "", "");
        }
        public static bool SendMail(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
        {
            bool mailSent = false;
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = SPContext.Current.Site.WebApplication.
                OutboundMailServiceInstance.Server.Address;
                MailMessage mailMessage = new MailMessage(From, To, Subject, Body);
                if (!String.IsNullOrEmpty(Cc))
                {
                    MailAddress CCAddress = new MailAddress(Cc);
                    mailMessage.CC.Add(CCAddress);
                }
                if (!String.IsNullOrEmpty(Bcc))
                {
                    MailAddress BCCAddress = new MailAddress(Bcc);
                    mailMessage.Bcc.Add(BCCAddress);
                }
                mailMessage.IsBodyHtml = IsBodyHtml;
                smtpClient.Send(mailMessage);
                mailSent = true;
            }
            catch (Exception) { return mailSent; }
            return mailSent;
        }

    }
}


  If the SharePoint context is not available utilize the SPWebApplication reference from a new SPSite object :

public static string GetSharePointMailService(string mysite)
 {
 string address;
 using (SPSite site = new SPSite(mysite))
 {
 address = site.WebApplication.OutboundMailServiceInstance.Server.Address;
 }
 return address;
 }


Option 2 :

Utilize the embedded SharePoint SendEmail. This has reduced capabilities, but is as straightforward as possible, and is the preferred approach if you simply want to send e-mail.

SPUtility.SendEmail(web, useHtml, htmlEncode, to, subject, htmlBody)


 Option 3 :

Sending E-Mail from a WCF Service

try
 {
 using (SPSite site = new SPSite(“http://server name”))
 {
 SPWeb myWeb = site.RootWeb;
 {
 string to = “someone@Someone.com”;
 string subject = “My Subject Message”;
 string body = “My Message Body”;
 HttpContext curCtx = HttpContext.Current;
 HttpContext.Current = null;
 bool success = SPUtility.SendEmail(myWeb, true, true, to, subject, body);
 HttpContext.Current = curCtx;
 }
 }
 }
 catch (Exception ex)
 {// handle error here!!!}

In office 365, you have ability to use SANDBOX solutions so the above classes are not supported. I used this trick to send email in the online environment using Sharepoint designer workflows


Option 4:

Use SP Designer to create email notifications. Quick and Easy and can be conditional too.



You could also create a reusable workflow for a content Type and associate with any list which contains that content type.


 

No comments:

Post a Comment