Friday, June 5, 2009

Sending email from SharePoint

By Hristo Yankov

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.
Bookmark and Share

4 comments:

gianluca said...

hi,
is it possible to have a complete working class with this code?
i'd like to implement this as feature in sharepoint, trough vs and wspbuilder, but i'm not a good "code writer"

Anonymous said...

Then research and learn.

Anonymous said...

You are wrong, you dont need a context, you just need the "web" and you can get the web inside a Execute method of a timer, without using the context.

Anonymous said...

Sorry I need some help here.
I'm a site admin for a sharepoint but couldn't see the setting options for "Operation" on Sharepoint 2007...do i need further rights on the site before i can implement this?
Thanks