How to send email via Sharepoint

This is a useful function for sending a message via Sharepoint:

private void SendEmail(SPWeb oWeb, bool appendHtmlTag, bool htmlEncode,
            String toAddress, String subject, String message, bool elevated)
        {
            //check if external email is set
            bool isExternalEmailSet = SPUtility.IsEmailServerSet(oWeb);
            if ((elevated == true) && (isExternalEmailSet == true))
            {//run elevated
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPUtility.SendEmail(oWeb, appendHtmlTag, htmlEncode, 
toAddress, subject, message);
                });
            }
            else if ((elevated != true) && (isExternalEmailSet == true))
            {//run un-elevated
                SPUtility.SendEmail(oWeb, appendHtmlTag, htmlEncode, 
toAddress, subject, message);
            }
        }
Categories: ASP.NET, C#, Sharepoint 2007
Tags: