Friday, May 30, 2008

Sending mail using SPUtility.SendEmail method

Hi All,

There are many ways to send an email from code. One of the way is to use SPUtility class.

Here is a brief introduction of this method:

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.

try
{
SPWeb thisWeb = SPControl.GetContextWeb(Context);
if (SPUtility.IsEmailServerSet(thisWeb ))
{
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
}


That's it. Your job is done.

But I think the good option is to use one more method before using SendMail is that check if the mail server is properly configuerd for Web Application or not. this can be done by usign SPUtility.IsEmailServerSet method which returns true if server is configuerd with SMTP mail settings.

For making Due diligence, i removed Outbound SMTP Server from Outgoing e-mail settings in Operations from central administration and come to know that it just works fine.

If you dont have that configuerd the condition will be false and mail will not be sent.


For more reference : Click Here

Thank you

4 comments:

Anonymous said...

Hi Malay,

Are you able to find a way to send mails to exteranl mail servers such as to gmail (xxx@gmail.com) or yahoo(xxx@yahoo.com/xxx@yahoo.co.in) using the SendMail() method?

Thanks,
Amar
amarendra.sonti@gmail.com

Anonymous said...

Hi Amar,
here you want to send mail to external servers like gmail,yahoo or send mail from external server?

Malay Vasavada said...

Hi Amar,

No you cannot send mail to external mail server.

If you want to send to external instead of this utility you can use System.Net.Mail class and send mail.

Anonymous said...

hmmm ur giv me grt help to develop may sharepoint Apllication,, thankzz man
Gud luck




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com