Many a times we need to send an email by code from the SharePoint Web Part. The following function shall help you in doing so.
private static void sendEmail(string subject, string Body, string _siteUrl, string _ToEmailAddress)
{
try
{
using (SPSite site = new SPSite(_siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPUtility.SendEmail(web, false, false, _ToEmailAddress, subject, Body);
});
}
}
}
catch (Exception ex)
{
SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("PHM Portal Event: ", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
}
}