Programatically Send E-mail in Sharepoint 2010

In this post I will discuss various methods of Sending an e-mail in SharePoint 2010.

Method 1 – Code retrieves the SMTP host’s address from the SharePoint configuration database.

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;
}

This ensures that you get the advantages of.NET-style mail delivery while keeping your configuration in Central Administration.
If however, you are not working in a SharePoint context, you can get the SPWebApplication reference from a new SPSite object like below

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

Method 2 : – This uses the embedded SharePoint SendEmail which has fewer 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)

The Paramets are :
web(SPWeb) – An object that represents the site
headers(StringDictionary) – A collection of additional headers
useHtml(bool) – Used to append an HTML tag to the message (true)
htmlEncode(bool) – Encodes the message and replaces characters in HTML tags with entities
to(string) – The address to which to send the e-mail
subject(string) – Contains the subject for the e-mail message
htmlBody(string) – Contains the body of the e-mail message
addFooter(bool) – Used if there is a footer to be appended to the e-mail

The headers parameter is used instead of the to and subject parameters. It allows you to set all possible mail headers and forces you to add at least the to header that way. An example follows:

StringDictionary headers = new StringDictionary();
headers.add(“to”,”authors@apress.com”);
headers.add(“cc”,”joerg@krause.net”);
headers.add(“bcc”,”checkthis@apress.com”);
headers.add(“from”,sp2010@apress.com);
headers.add(“subject”,”Send an EMail from SPUtility”);
headers.add(“content-type”,”text/html”);
string bodyText =”This is an html formatted message.”;
SPUtility.SendEmail(web, headers, bodyText);
Note that StringDictionary is defined in the System.Collections.Specialized namespace.

Method 3 : - Sending E-Mail from a WCF Service

Sending an e-mail from a service when SPContext is not available could fail. As a workaround, you have to prevent the mail function from reading the current context by using HttpContext.Current = null. If it can’t, it will retrieve the right context and it will then work. The next example shows this.

Sending E-mail Using SendMail from a WCF Service
try
{
using (SPSite site = new SPSite(“http://sharepointserve”))
{
SPWeb thisWeb = site.RootWeb;
{
string to = “someone@apress.com”;
string subject = “Book Message”;
string body = “A message from SharePoint”;
HttpContext curCtx = HttpContext.Current;
HttpContext.Current = null;
bool success = SPUtility.SendEmail(thisWeb, true, true, to, subject, body);
HttpContext.Current = curCtx;
}
}
}
catch (Exception ex)
{// Exception handling skipped for clarity}

In the above code the current context is set to null to force the context to be retrieved again. Saving the current context ensures that the service works properly after the method has been executed.

Related Posts

Disclaimer: The contents of this site are provided “AS IS”. Information and views expressed including URL(s) and other Internet website references, may change without notice. You bear the risk of using it. SharePoint, MOSS, SharePoint 2010, SharePoint 2013 and Microsoft are trademarks of the Microsoft Corporation.Learning SharePoint only provides Tips,Tricks and Tutorials for using Microsoft SharePoint, Microsoft Office and related Softwares.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.