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