I'm going to be putting in alot of code for a really simple process. Generally I create a separate class that handles emails for my code...Actually this was created for my organization from a MS consultant. Then I can call any function in this class and I put in an example. I also use an AppSetting in the web.config, and I included that line at the bottom.
//----------------------Mail Class----------------------------------------
/// <summary>
/// This class is used to send emails (via CDONTS) from within the CMS system. The system requires a
/// valid SMTP server, and this address is controlled through the web.config AppSetting "MailServer".
/// </summary>
/// <remarks>Author: Simon Chester (Microsoft) March 2003</remarks>
public class MailHandler
{
private ErrorHandler _err;
private MyCompanyDirectory _hrDir;
private string _mailServer;
const string ERROR_PAGE_URL = "/MyCompany/ErrorPages/MailError.aspx";
/// <remarks>Access Type: Public</remarks>
/// <summary>
/// This is the constructor for the MailHandler class.
/// </summary>
public MailHandler()
{
_err = new ErrorHandler();
_hrDir = new MyCompanyDirectory();
_mailServer = ConfigurationSettings.AppSettings["MailServer"];
}
/// <remarks>Access Type: Public</remarks>
/// <summary>
/// This function is used to trigger an email to the author of a posting
/// when the editor declines it. The function is called from the Declined
/// event in the global.asax.
/// </summary>
/// <param name="thisPosting">(Microsoft.ContentManagement.Pu blishing.Posting) The posting object which has been declined.</param>
/// <param name="serverName">(string) The name of the server on whihch the posting
/// resides.</param>
/// <param name="response">(System.Web.HttpResponse) The HttpResponse Object. Used to redirect the user
/// if an error occurs.</param>
/// <param name="editorName">(string) The full name of the editor. This should be in
/// the form of: WinNT://DomainName/UserName.</param>
public void SendDeclineEmail(Posting thisPosting, string serverName, System.Web.HttpResponse response, string editorName)
{
string author;
string editor;
string subject;
string body;
//get the authors email address from the AD system via GetEmailAddress
author = GetEmailAddress(thisPosting.LastModifiedBy.ClientA ccountName);
//if an address is found continue
if(author!=null)
{
//get the editors email address
editor = GetEmailAddress(editorName);
//if an address is found, continue
if(editor!=null)
{
//construct the subject and body of the mail address, including a
//link to the posting
subject = "Declined: A posting was declined in the "+ thisPosting.Parent.DisplayName + " channel.";
body = "The following item was declined:<br><br>";
body+= "<a href='http://" + serverName + thisPosting.UrlModeUnpublished + "&wbc_purpose=Basic'>"+ thisPosting.DisplayName +"</a>";
//Send the email
SendMail(author, editor, "", subject, body);
}
//if an editor email could not be found, present the error to the user
else
{
DisplayError(serverName, thisPosting, response);
}
}
//if an author email could not be found, present the error to the user
else
{
DisplayError(serverName, thisPosting, response);
}
}
/// <remarks>Access Type: Private</remarks>
/// <summary>
/// This function redirects the user to an error page to inform them that the email was
/// not sent and to display the reason. A link is also provided to continue.
/// </summary>
/// <param name="serverName">(string) This is the name of the server which was processing the request.</param>
/// <param name="thisPosting">(Microsoft.ContentManagement.Pu blishing.Posting) The posting object of the page that was being processed.</param>
/// <param name="response">(System.Web.HttpResponse) The HttpResponse object. This is used to redirct the user
/// to an error page.</param>
private void DisplayError(string serverName, Posting thisPosting, System.Web.HttpResponse response)
{
string nextUrl = "http://" + serverName + thisPosting.UrlModeUnpublished;
string errorMessage = "The email to " + thisPosting.LastModifiedBy.ClientAccountName + " was not sent. The error has been logged on the server.<br>Error Message:<br>" + _hrDir.LastKnownError;
response.Redirect(ERROR_PAGE_URL + "?nexturl=" + nextUrl + "&error=" + errorMessage);
}
/// <remarks>Access Type: Private</remarks>
/// <summary>
/// This function calls out to the MyCompanyDirectory class to grab the email address
/// for the person passed in. If the email address cannot be found, the function will
/// log the error in the system log.
/// </summary>
/// <param name="username">(string) The full username for the person the address must be found for.
/// This should be in the form of; WinNT://DomainName/UserName</param>
/// <returns>(string) Valid elmail address in the form of;
[email protected]</returns>
public string GetEmailAddress(string username)
{
string emailAddress;
emailAddress = _hrDir.GetEmailBySAM(username);
if(emailAddress==null)
{
// return _hrDir.LastKnownError;
_err.LogError(_hrDir.LastKnownError, "MailHandler.GetEmailAddress", EventLogEntryType.Error, true);
emailAddress = null;
}
return emailAddress;
}
/// <remarks>Access Type: Public</remarks>
/// <summary>
/// This function is used to actually trigger the email. The function uses
/// System.Web.Mail to fire the message, which currently wraps around
/// CDONTS. The System requires a valid SMTP server, the name of which is held
/// in the AppSettings section of web.config, under the "MailServer" key.
/// </summary>
/// <param name="to">(string) Email recipient. Can be multiple people seperated by a semi-colon</param>
/// <param name="from">(string) Email sender</param>
/// <param name="subject">(string) Subject line for the email.</param>
/// <param name="body">(string) Main body of the email.</param>
/// <returns>(string) Error message if the mail fails to send.</returns>
public string SendMail( string to, string from, string cc, string subject, string body)
{
string error = "";
try
{
MailMessage newMail = new MailMessage();
newMail.To = to;
newMail.From = from;
newMail.Cc = cc;
newMail.Subject = subject;
newMail.Body = body;
newMail.BodyEncoding = System.Text.Encoding.ASCII;
newMail.BodyFormat = MailFormat.Html;
string str = SmtpMail.SmtpServer;
SmtpMail.SmtpServer= _mailServer;
SmtpMail.Send(newMail);
}
catch(Exception ex)
{
error = ex.Message;
//throw ex;
}
return error;
}
}
//---------------------Call to function in Mail Class --------------------
//send email to user confirming username & pwd
string sTo = useremail;
string sFrom = "
[email protected]";
string sSubj = userdesc + " Request for Application Permissions";
string sCC = "
[email protected]";
//build email body
StringBuilder _sb = new StringBuilder();
_sb.Append("Dear MyCompany User:");
_sb.Append("<p>Your user account has been activated or modified.");
_sb.Append("<p>Please use the following username and password to login to <a href=http://MyCompany.MyCompany.com/>MyCompany</a>: ");
_sb.Append("<p>Username: " + userlogin);
_sb.Append("<p>Password: " + _random);
_sb.Append("<p><b>After you login, please change your password by selecting Applications, Change My Password. Also, please review the <a href=http://MyCompany.MyCompany.com/departments/legal/legalinfo/site_usage_info_agreement.htm>Site Usage & Information Agreement</a>.</b>");
_sb.Append("<p>We hope you will find the following links useful:</p>");
_sb.Append("<p><a href='http://MyCompany.MyCompany.com/Resources/Help/UserTools/login_help.htm'>Login Help</a></p>");
_sb.Append("<p><a href='http://MyCompany.MyCompany.com/Resources/Help/UserTools/forgot_password.htm'>Forgot Password?</a></p>");
_sb.Append("<p><a href='http://MyCompany.MyCompany.com/Resources/Help/FAQs/'>Frequently Asked Questions</a></p>");
_sb.Append("<p><a href='http://MyCompany.MyCompany.com/Resources/Help/UserTools/edit_your_user_profile.htm'>Edit User Profile</a> (to add or remove applications)</p>");
_sb.Append("<p>If you need additional assistance, simply reply to this email or call 1-xxx-xxx-xxx.</p>");
_sb.Append("<p>Regards,</p>");
_sb.Append("<p>MyCompany Security Administrator</p>");
MailHandler mh = new MailHandler();
mh.SendMail(sTo,sFrom,sCC,sSubj,_sb.ToString());
//---------------------line to include in config file --------------------
<add key="MailServer" value="smtpmail.MyCompany.com" />