Email in Castle Monorail 2.0

Castle Monorail has been my web framework of choice for some years now. I’ve been using the obsolete RenderEmailAndSend method as that was how it is done in the sample code. Monorail 2.0 uses Core 1.2 which features an integrated email sender component, and the old sample code does not work anymore since Castle.Components.Common.EmailSender.Message has been removed.

The “new” way of sending isn’t documented officially anywhere, hence this blog post.

First, you’ll want a NVelocity template in the ViewsMail folder. We’ll call it Hello.vm:

subject: Hello!
from: $from
to: $to

Hello $name, this is a message from $site

Now for the controller, which we’ll name EmailController.cs (I’m not very imaginative):

using Castle.MonoRail.Framework;

namespace MonorailEmailSample.Controllers
{
    public class EmailController : Controller
    {
        public void Send()
        {
            PropertyBag["from"] = "myemail@domain.com";
            PropertyBag["to"] = "you@myfriend.com";
            PropertyBag["name"] = "Your Name";
            PropertyBag["site"] = "http://mywebsite.com/";

            DeliverEmail(RenderMailMessage("hello", null, PropertyBag));

            RenderText("Email sent!");
        }
    }
}

That’s it. The RenderMailMessage method signature I’m demonstrating is RenderMailMessage(string templateName, string layoutName, IDictionary parameters). We don’t need a layoutName if we are sending a plain text email which we are doing, hence we are passing in null.

Of course, you’ll want to set up your SMTP server properly in your monorail config section as per the instructions.

Leave a Reply

Your email address will not be published. Required fields are marked *