There are multiple ways to handle mailing in a .net application. System.Net.Mail is a well known assembly we often use for sending simple email messages. I am building a fairly complex website with couple of different workflows [nothing to do with Windows Workflow] into it. In each workflow, the website could be sending multiple emails with different text. I wanted something maintainable and flexible. MvcMailer is a very good open source library by SM Sohan to send mails using Razor views. Razor Views!! yes, I am already sold on this. It also supports other view engines btw. There is a good documentation on the gitHub and it details all the scenarios. It is so cool and has already been on Scott Hansleman’s blog. If you want to pass in strongly typed view then you can check this stackoverflow answer by Darin Dimitrov. Again very cool. I like the idea of strongly typing and I have no other choice due to sheer complexity of the web application.
I wanted to share some of the lessons I learned in sending multiple emails with different content to different types of users. Some of the things here is not related to coding but is very important in keeping yourself organized. I tend to work backwards focusing on what the user will see. So I first start documenting an Email Template in word document and keep adding them as new ones pop-up. And I follow most of the steps shown below.
1. Maintain a word document with Email Templates.
This is applicable only if you are building a fairly complex MVC application with different stake holders. It is very important to get it reviewed by different people involved in the project. Sometimes you might get the text wrong and may be the intent is not conveyed properly to the end user. I am not perfect at English and I like to get it reviewed by someone. I am going to share some tidbits of such a word document.
Template Name – Send Email to Vendors for product pickup
TemplateId – ET0001
Description
The following email will be sent to all the recipients listed below after all the products requested ready for shipping. After all the requested products are packaged, an order is considered as ready for shipping.
Recipients
1. To the shipping vendor to notify about product pickup for shipping.
Subject
Order ready for pickup
Email Body
[Shipping Vendor’s Full Name],
Your next products order has been ready for pickup and you can come on and no later than [pickup date]
Products Ordered [List Dynamically Populated]
1. Gel
2. Shaving Cream
3. Toothbrush
If you have, any questions or concerns related to your order pickup please contact [contact email address].
[Signature]
The purpose of this type of word document is to make sure that all the content you put in front of the user is appropriate for its intended audience. Is the wording right? Is there any misleading information? Are we conveying our intent to the user in a clear and concise manner? All these and more questions need to be ironed out before you ship this feature. Because later down the road it will be difficult when you have say 25 or 50 different Razor Views, ViewModels sending emails. Right now my application has not reached to that level of complexity but I do see it reaching it. All the text that is underlined in the Email Body will be replaced by strongly typed ViewModel. The Email Body will closely resemble the Razor Views in the Web Application. Once you know the structure of all the emails you can start coding Razor Views. So again it reviewed or proof checked by Project manager or immediate supervisor.
2. Use Strongly Typed Views
After I have my text templates ready, I can directly copy them into the web application. I scaffold my views as necessary using the Scaffold command. From point 1 we can create our Strongly Typed View named ET001 [TemplateId].
@using WebsiteName.ViewModels
@model OrderPickupEmailViewModel
@Model.VendorFullName,
Your next products order has been ready for pickup and you can come on and no later than @Model.PickupDeadline
Products Ordered
<ul>
@foreach (var item in @Model.Products)
{
<li>@item</li>
}
</ul>If you have, any questions or concerns related to your order pickup please contact @Model.ContactInfo
@Model.Signature
3. Use ViewModels to pass information to Razor Views.
After I construct my Razor Views I know what Properties I should be adding to my ViewModel. I then create my ViewModel named OrderPickupEmailViewModel see point 2.
public class OrderPickupEmailViewModel
{
public string ToAddress {get;set;}
public string Subject {get;set;}
public string VendorFullName{get;set;}
public DateTime PickupDeadline {get;set;}
public List<string> Products {get;set;}
public string ContactInfo {get;set;}
public string Signature {get;set;}
}
4. Modify the Interface and Class to accept your custom ViewModels
After the ViewModels are ready I go and modify the interface IUserMailer.cs methods to use my CustomViewModels.
public interface IUserMailer
{
MvcMailMessage OrderPickupConfirmation(OrderPickupViewModel ordp);
}
After that I modify the method inside UserMailer.cs created as a part of scaffolding.
public virtual MvcMailMessage OrderPickupConfirmation(OrderPickupViewModel ordp)
{ViewData = new ViewDataDictionary(ordp);
return Populate(x =>
{
x.Subject = ordp.Subject;
x.ViewName = "RequestSubmissionConfirmation";
x.To.Add(ordp.ToAddress); });
}
Finally send the email in the respective controller.
[HttpPost]public ActionResult SendEmail(OrderPickupViewModel model){UserMailer.OrderPickupConfirmation(model).Send();}
5. Keep common settings inside your web.config file just like MvcMailer stores smtp settings inside web.config file. You can use web.config transforms to send emails to a redirectfolder and to end users when in production.
If you are following any techniques or best practices to keep your self then please share in the comments below. I would like to know and get better.
No comments:
Post a Comment