Sunday, April 17, 2011

Send Email in SharePoint via .NET SmtpClient Class and SharePoint SPUtility Class

In SharePoint, you could send email programmatically by using either .NET Class Library or SharePoint Object Model. I always prefer the second method since using SharePoint ensures that the required settings are maintained by Central Administration.

The following show the two code examples on how you could do this:

Sending Email via .NET SmtpClient Class

using System.Net.Mail;
using Microsoft.SharePoint;

/// <summary>
/// Sends the mail via NET SmtpClient.
/// </summary>
/// <param name="Subject">The subject.</param>
/// <param name="Body">The body.</param>
/// <param name="IsBodyHtml">if set to <c>true</c> [is body HTML].</param>
/// <param name="From">From.</param>
/// <param name="To">To.</param>
/// <param name="Cc">The cc.</param>
/// <param name="Bcc">The BCC.</param>
/// <returns></returns>
public static bool SendMailviaNET(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;
}

Sending Email via SharePoint SPUtility Class

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;

/// <summary>
/// Sends the mail via SharePoint SPUtility.
/// </summary>
/// <param name="Subject">The subject.</param>
/// <param name="Body">The body.</param>
/// <param name="IsBodyHtml">if set to <c>true</c> [is body HTML].</param>
/// <param name="From">From.</param>
/// <param name="To">To.</param>
/// <param name="Cc">The cc.</param>
/// <param name="Bcc">The BCC.</param>
/// <returns></returns>
public static bool SendMailviaSharePoint(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
{
    bool mailSent = false;
    try
    {
        SPWeb thisWeb = SPContext.Current.Web;
               
        StringDictionary headers = new StringDictionary();
        headers.Add("to", To);
        headers.Add("cc", Cc);
        headers.Add("bcc", Bcc);
        headers.Add("from", From);
        headers.Add("subject", Subject);
        if (IsBodyHtml) headers.Add("content-type", "text/html");

        mailSent = SPUtility.SendEmail(thisWeb, headers, Body);
    }
    catch (Exception)
    {
        return mailSent;
    }
    return mailSent;
}

No comments: