SPUtility Methods


SPUtility.SendEmail Method (Microsoft.SharePoint.Utilities)
Sends the e-mail message to the specified address.
Overload List

Name Description
SPUtility.SendEmail (SPWeb, StringDictionary, String)
Sends the e-mail message to the specified address.
SPUtility.SendEmail (SPWeb, StringDictionary, String, Boolean)
Sends the e-mail message to the specified address.
SPUtility.SendEmail (SPWeb, Boolean, Boolean, String, String, String)
Sends the e-mail message to the specified address.
SPUtility.SendEmail (SPWeb, Boolean, Boolean, String, String, String, Boolean)
Sends the e-mail message to the specified address.
See Also

Tags :


Community Content

Content Master Ltd
SPUtility.SendEmail

Description

The SPUtility.SendEmail method enables you to send an email from the context of a SharePoint Web (SPWeb) object. The SMTP settings associated with the SPWeb object are used to send the email. The method returns a boolean value that represents whether the email was sent successfully.

There are four overloaded methods that support different parameter lists. Two of the overloaded methods support StringDictionary objects that are used to specify the message headers (such as the To, Cc, and Subject fields), while the other two overloaded methods enable you to pass the To and Subject fields as simple String objects.

Usage Scenario

You can use the SPUtility.SendEmail method to send emails for a variety of different scenarios, such as building a Web-based form to enable a user to send an email from a SharePoint application without requiring them to start their email client application, or incorporating email notifications as part of a business process or workflow in a SharePoint site.

The following code samples show how to use the SPUtility.SendEmail method.

C# Code Sample

try
{
SPWeb thisWeb = SPControl.GetContextWeb(Context);
string toField = "someone@microsoft.com";
string subject = "Test Message";
string body = "Message sent from SharePoint";
bool success = SPUtility.SendEmail(thisWeb,true, true, toField, subject, body);
}
catch (Exception ex)
{
// handle exception
}

Visual Basic.NET Code Sample

Try
Dim thisWeb As SPWeb = SPControl.GetContextWeb(Context)
Dim toField As String = "someone@microsoft.com"
Dim subject As String = "Test Message"
Dim body As String = "Message sent from SharePoint"
Dim success As Boolean = SPUtility.SendEmail(thisWeb, True, True, toField, subject, body)
Catch ex As Exception
' handle exception
End Try
Tags : sharepoint

kukdai
Issues/Bugs

The body text parameter must have a new line character at least every 2048 characters.
In some cases, the subject parameter cannot contain a comma "," character.

See here: http://rrfreeman.blogspot.com/2009/09/sputilitysendemail.html

Can't we add different email address as the sending address which will appear as the replyto address if somebody replied this email????


djonexx
Sending mail from a service

This method works properly as documented only when it is called from within the code executing in a frontend web site. It does not work from the context of a WCF or WebService method call executing in the application server, or any other place which is within an HttpContext but not a SPContext.

To work around this, one should set the HttpContext.Current to null before calling the method, and restore it after.

The code sample below shows how to achieve this:

C# Code Sample

try
{
SPSite thisSite = new SPSite(http://sharepoint/site);



SPWeb thisWeb = thisSite.RootWeb;
string toField = "someone@microsoft.com";
string subject = "Test Message";
string body = "Message sent from SharePoint";
HttpContext oldContext = HttpContext.Current;
HttpContext.Current = null;



bool success = SPUtility.SendEmail(thisWeb, true, true, toField, subject, body);
HttpContext.Current = oldContext;


}
catch (Exception ex)
{
// handle exception
}

Page view tracker