Skip to content
Mark McDonald edited this page Mar 18, 2019 · 7 revisions

Step 1

Make your controller and action look something like this. Note that Email is assigned to a dynamic object, so we can stuff any data we like into it.

using Postal;

namespace App.Controllers {

    public class ExampleController : Controller
    {
        public ActionResult Send(string message)
        {
            dynamic email = new Email("Example");
            email.To = "[email protected]";
            email.Message = message;
            email.Send();

            return RedirectToAction("Sent");
        }
    }
}

Step 2

In your project's "Views" folder, create a folder called "Emails".

Step 3

Create a new view in ~\Views\Emails called "Example.cshtml". The name of the view matches the "Example" string we passed to the Email constructor.

To: @Html.Raw(ViewBag.To)
From: [email protected]
Subject: An example message

Hello,
The message is: @ViewBag.Message

Step 4

Edit your web.config. Postal uses the built-in SmtpClient of .NET, so the usual config section is used. This example will save emails to disk, rather than sending, this is great for testing purposes.

<system.net>
    <mailSettings>
        <smtp deliveryMethod="SpecifiedPickupDirectory">
            <specifiedPickupDirectory pickupDirectoryLocation="c:\email"/>
            <network host="localhost"/>
        </smtp>
    </mailSettings>
</system.net>

Important Notes

  • We're using the dynamic ViewBag object in the view to access our email data.
  • The email 'headers' are in the view.
  • There must be a blank line between the headers and the body.
  • The Layout property for the email views should probably be null, or at least not that used by your actual web page views. Create a file called ~/Views/Emails/_ViewStart.cshtml (This file is included in the Postal nuget package) to override the Layout for all email views:
@{ Layout = null; }
Clone this wiki locally