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

Feedbacks

 avatar
#1
Community Server Daily News
09.01.2006 @ 1:22 PM
from the editor occasional messages that don't fit anywhere else We'll be adding a weekly recap of the
 avatar
#2
Bob Mixon
07.10.2007 @ 11:45 PM
Greetings Keyvan, Great information, I appreciate it. I am new to CS 2007 and would like to understand the "best practice" for modifying the e-mail sent to a user just after they have joined. Ultimately my goal is to include their password. The reason for this is; in many situations, I am creating the account and assigning their initial password. I would like it included with additional text asking them to change it after they log in. Can I get you to point me in the right direction here? Where would I find this template and what additions do I need to make to include their password. Thank you in advance for the support! Sincerely, Bob Mixon Microsoft SharePoint (MOSS) MVP http://www.BobMixon.com http://www.BobMixonConsulting.com
admin avatar
#3
Keyvan Nayyeri
07.11.2007 @ 6:56 AM
Bob, Leave me an email. Then and I can give you some more information.
 avatar
#4
Keyvan Nayyeri
09.08.2007 @ 11:22 AM
Back in September 2006 I wrote a CS Dev Guide post about emailing in Community Server 2.1 . In Community
 avatar
#5
TunnelRat
11.29.2007 @ 3:52 PM
This doesn't work with CS 3.x. The EmailQueueProvider.Instance().QueueEmail method now takes 2 arguments, and their is no Emails.SendQueuedEmails method. Where can I find an example that works with 3.x?

Leave a Comment