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:
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
Hi Amar,
here you want to send mail to external servers like gmail,yahoo or send mail from external server?
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.
hmmm ur giv me grt help to develop may sharepoint Apllication,, thankzz man
Gud luck
Post a Comment