Email in Castle Monorail 2.0

Cas­tle Mono­rail has been my web frame­work of choice for some years now. I’ve been using the obso­lete Ren­derE­mai­lAnd­Send method as that was how it is done in the sam­ple code. Mono­rail 2.0 uses Core 1.2 which fea­tures an inte­grated email sender com­po­nent, and the old sam­ple code does not work any­more since Castle.Components.Common.EmailSender.Message has been removed.

The “new” way of send­ing isn’t doc­u­mented offi­cially any­where, hence this blog post.

First, you’ll want a NVe­loc­ity tem­plate in the Views\Mail folder. We’ll call it Hello.vm:

sub­ject: Hello!
from: $from
to: $to

Hello $name, this is a mes­sage from $site

Now for the con­troller, 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"] = "tzinmein@kidotech.com";
            PropertyBag["name"] = "Your Name";
            PropertyBag["site"] = "http://mywebsite.com/";

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

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

That’s it. The Ren­der­MailMes­sage method sig­na­ture I’m demon­strat­ing is RenderMailMessage(string tem­plate­Name, string lay­out­Name, IDic­tionary para­me­ters). We don’t need a lay­out­Name if we are send­ing a plain text email which we are doing, hence we are pass­ing in null.

Of course, you’ll want to set up your SMTP server prop­erly in your mono­rail con­fig sec­tion as per the instruc­tions.

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.