Although in general I believe that SharePoint should contribute
to reducing email rather than adding to it, there are circumstances and user
requirements that call for SharePoint to produce emails. I have used code
similar to the following function to create and send emails from SharePoint as
needed.
// Abbreviated namespace imports
using System.Collections.Specialized;
// For StringDictionary
using System.Text; // For StringBuilder
using Microsoft.SharePoint.Utilities;
// For
SPUtility
/// <summary>
/// Email a message to a user
/// </summary>
/// <param name=”emailSubject”>string: the subject for the email</param>
/// <param name=”emailAddr”>string: the target email address</param>
/// <param name=”emailMsg”>string: the message to include in
the email</param>
private void EmailUser(string emailSubject, string emailAddr, string emailMsg)
{
try
{
// Define a dictionary for the email parameters
StringDictionary headers = new StringDictionary();
// Set the email content type (I am using text format in this
example)
// Use "text/html" if you
want to format/send email as HTML
headers.Add("content-type", "text/plain");
// Set the from address (does not need to be a valid email
address)
headers.Add("from", "donotreply@yourdomain.local");
// Set the to address
headers.add("to", emailAddr);
// Set the cc address (in this
case we will cc the current SharePoint user)
headers.add("cc", SPContext.Current.Web.CurrentUser.Email);
// Set the bcc address (if
desired)
headers.add("bcc", "someaddr@yourdomain.local");
// Set the email subject
headers.add("subject", emailSubject);
// Build the message body (just a
sample with text line feeds and tabs)
StringBuilder emailBody = new StringBuilder();
emailBody.AppendFormat("*** SharePoint Notification ***\n\n");
emailBody.AppendFormat("Site:\t{0}\n", SPContext.Current.Web.Url);
emailBody.AppendFormat("Message:\t{0}\n", emailMsg);
emailBody.AppendFormat("\n*** End of SharePoint Notification ***");
// Send the email
SPUtility.SendEmail(SPContext.Current.Web, headers, emailBody.ToString());
} // end try
catch (exception ex)
{
// Log exception to event and/or
uls logs (code omitted)
// Handle exception (code
omitted)
} // end catch
} // end EmailUser
|
No comments:
Post a Comment