Sending email from SharePoint

Did you ever need to send an email out, from your SharePoint custom web application? When you have such a task, perhaps the first idea that you have is to use System.Net.Mail namespace. However, this requires that your application maintains a setting for the SMTP server, reply address and etc. Wouldn’t it be easier if you could delegate the task of storing this configuration to SharePoint (and its Administrator) and instead, just focus on sending out the actual email?


Your solution is the Microsoft.SharePoint.Utilities.SPUtility class! It has the very convenient method ‘SendEmail‘. This is basically SharePoint’s native functionality for email delivery.

The first thing you (or the SharePoint Administrator) need to do is setup the Outgoing email SMTP server. Open Central Admin -> Operations -> Outgoing E-Mail Settings.

There, you need to set the Outbound SMTP server, From address and Reply-to address.

And this is exactly how you delegate SMTP setting storage to SharePoint. Now, how do you actually send email from your code?

First, it is always a good idea to check if the email server is set:
bool isEmailServerSet = SPUtility.IsEmailServerSet(web);

If this returns false, you should not bother trying to send the email. Instead, show an error message or notify the SharePoint administrator, to check the settings of the server. If it returns true, you are good to go:
SPWeb web = SPContext.Current.Web;
bool appendHtmlTag = false;
bool htmlEncode = false;
string toAddress = “test@example.com”;
string subject = “Subject”;
string message = “Message text”;
bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);

In some cases, you may need to run this code with elevated privileges:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
});

SendEmail method returns a boolean, indicating if sending the email was successful or not.

However, there is a catch in using SPUtility.SendEmail. Internally this method relies on having a SPContext. In other words – if you are trying to use SPUtility.SendEmail from a SharePoint timer, it will fail, because there will be no context. This is when you have no other choice, but use System.Net.Mail. But how can you still benefit from SharePoint storing your SMTP settings? You do it this way:

SPWeb web = new SPSite(“http://example”).RootWeb;
string toAddress = “test@example.com”;
string subject = “Subject”;
string messageText = “Message text”;
string replyTo = web.Site.WebApplication.OutboundMailReplyToAddress;

MailMessage message = new MailMessage(replyTo, toAddress, subject, messageText);
message.IsBodyHtml = false;

string smtpAddress =
web.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

SmtpClient smtp = new SmtpClient(smtpAddress);
smtp.Send(message);

So what did we do here? We use .NET’s native MailMessage and SmptClient classes, but we still read the configuration from the SharePoint site we opened by URL.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: