CS Dev Guide: Send Emails
Any web application needs a way to send emails to different kinds of its users. This capability is provided in Community Server from early versions. Sending emails is one of easiest parts of Community Server development.
To send emails in Community Serve you have two options: using pre-defined email templates for some common situations or create an email manually.
First option is easy to implement. Community Server has provided several default templates for your emails and you can add your own templates via resource files as well. CommunityServer.Components.Emails namespace has many methods that get some parameters then create and send an email based on the template they have.
For example you can send a notification email to user to let him know his account is created.
private void SendEmail(string username, string password
, string email)
{
User user = new User();
user.Username = username;
user.Password = password;
user.Email = email;
Emails.UserCreate(user, password);
}
These methods add emails to queue automatically. You can use Emails.SendQueuedEmails() method to send all queued emails to recipients. This method takes three parameters: an integer value for failure interval, an integer value that specifies the maximum number of tries of sending process failed and a SiteSettings object.
private void SendQueuedEmails()
{
Emails.SendQueuedEmails(5, 5,
CSContext.Current.SiteSettings);
}
Second option to send emails is manual option. This is useful when you want to send an email but it doesn't fit to Community Server pre-defined templates.
This option is very similar to sending emails in ASP.NET 1.x. You need to create an instance of System.Web.Mail.MailMessage object, set its appropriate properties and add it to emails queue using Community Server APIs. You know MailMessage object is obsolete in ASP.NET 2.0 but Community Server accepts old objects to be able to work under ASP.NET 1.1. If you need more information about sending emails in ASP.NET 2.0, read my post about my SMTP component, Gopi, from here.
Here is a sample of how to create a new MailMessage and add it to emails queue.
private void SendEmailManually()
{
MailMessage mail = new MailMessage();
mail.From = "sender@server.com";
mail.To = "receiver@server.com";
mail.Priority = MailPriority.Normal;
mail.BodyFormat = MailFormat.Text;
mail.Body = "Long Live Community Server!";
mail.Subject = "Test Email";
EmailQueueProvider.Instance().QueueEmail(mail);
}
You saw I used EmailQueueProvider.Instance().QueueEmail() method to add my email to queue. You can use EmailQueueProvider.Instance() object to deal with emails queue in Community Server. For instance you can remove an email from emails queue by passing its Guid to EmailQueueProvider.Instance().DeleteQueuedEmail() method.
private void DealWithEmails()
{
EmailQueueProvider.Instance()
.DeleteQueuedEmail(Guid.NewGuid());
}
Now playing: Modern Talking - You are not alone
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
5 Comments : 09.01.06

#1
Community Server Daily News
09.01.2006 @ 1:22 PM