Home

Welcome

ActionMailer.Net aims to be an easy, and relatively painless way to send email from your ASP.NET MVC application. The concept is pretty simple. We render HTML by utilizing some pretty snazzy view engines, so why can't we do the same thing for email?

This library is still in active development. I'd love to hear what you think! If you find any bugs, please don't hesitate to open an issue here on bitbucket. Pull requests and patches are always welcome, too.

Thanks JetBrains!

I just want to say thanks to JetBrains, the makers of the most awesome Visual Studio add-on, ReSharper. They have been gracious supports of ActionMailer.Net for the past year, and their tools have certainly helped me produce much higher quality code. Check them out!

Handy Screencast

I've recently recorded a screencast, so check it out on Youtube if you'd like. you can watch the video here:

http://www.youtube.com/watch?v=QQRzYo7k9Vs&hd=1

Installation

Easy as pie using NuGet. Inside Visual Studio, go to View > Other Windows > Package Manager Console, and then type:

Install-Package ActionMailer

Usage

This is the easy part, I swear! There are three steps on the way to email awesomeness:

Step 1

Create a new controller in your project. I called mine MailController, but any name will do. Inside your controller, each action will need to return EmailResult objects. The best way to do that is to use the handy Email() method. Here's some sample code to get you started.

public class MailController : MailerBase
{
    public EmailResult VerificationEmail(User model)
    {
        To.Add(model.EmailAddress);
        From = "no-reply@mycoolsite.com";
        Subject = "Welcome to My Cool Site!";
        return Email("VerificationEmail", model);
    }
}

Step 2

Now we need to create a View for this email. The view can use any ViewEngine you like, and it will even work with master pages (or layouts in Razor). The views live in the same place your normal views do.

There is one important difference between a normal ASP.NET MVC view and an ActionMailer.NET view. You need to make sure that the view's name specifies its format. Because ActionMailer.Net supports multi-part messaging, you can render views in HTML, plain text, or both.

To specify the format of your view, just append the format to the view's name. For the example email action above, if you want to send the email in plain text, your view's name should be VerificationEmail.txt. If you wish to send the email in HTML, then your view's name should be VerificationEmail.html. This means that if I'm using Razor, the full view file name should be VerificationEmail.txt.cshtml or VerificationEmail.html.cshtml.

Remember: Since ActionMailer.Net supports multi-part messaging, you can absolutely use both formats at the same time, and your message will be properly encoded.

With all that said, here's a sample plain text Razor view (with a file named VerificationEmail.txt.cshtml) that matches the nifty email action above.

@using ActionMailer.Net
@model User

@{
    Layout = null;
}

Welcome to My Cool Site, @Model.FirstName

We need you to verify your email.  Click this nifty link to get verified!

@Url.AbsoluteAction("Verify", "Account", new { code = @Model.EmailActivationToken.ToString() })

Thanks!

Step 3

Now it's just a matter of calling your action directly any time you need to send an email. You can call it like this:

new MailController().VerificationEmail(newUser).Deliver();

Configuration

There is a little bit of configuration that needs to be done, but it’s pretty easy. Under the hood, ActionMailer.Net is using System.Net.Mail.SmtpClient to send all your messages. This means that you need to add some configuration options in your project’s Web.config file. While I’m in development, I like to send all emails to a folder on my hard drive:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\temp\"/>
    </smtp>
  </mailSettings>
</system.net>

But later, when you move to production, you’ll need some more appropriate settings:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="smtp.mysite.com" userName="myuser" password="mypassword" />
    </smtp>
  </mailSettings>
</system.net>

Advanced Stuff

The astute reader will notice that MailerBase has a couple of virtual methods: OnMailSent and OnMailSending. Feel free to have fun with those :)

You might also notice that there is a DeliverAsync() method. I don't offer a callback here because even with Async delivery, the OnMailSent method will be called after the send operation completes.

Finally, take a look at the IMailSender interface. I provide a default implementation of this interface that uses System.Net.Mail.SmtpClient, however it should be very easy for you to implement your own version of this interface if you want to use something like Amazon SES or the SendGrid API to send stuff. Once you create your own implementation, just override the default in the constructor of your controller:

public MailController() {
    MailSender = new MyCustomMailSender();
}

Standalone Version

Documentation on the usage of the standalond version can be found at geeksharp.com/2011/07/06/actionmailer-0-6-released/

Contributing

The unit tests are based on TestDriven.NET. There is a Personal License available for open source developers. Once installed simply right click on the test project, or an individual test and click "Run Tests" with the TestDriven rocket icon.

Special Thanks

I'd like to thank the Rails project specifically for showing me that sending emails didn't have to suck. Without seeing ActionMailer, I would (probably) still be using StringBuilder to generate emails.

I'd also like to thank S. M. Sohan whose code I took some inspiration from (specifically the testing stuff). There were a few issues that stopped me from using his MvcMailer in my projects, but it's another good alternative for anyone that likes this system.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.