Wednesday, August 21, 2013

Email sending via MvcMailer

In ASP.Net MVC 3/4, MvcMailer provides you with an ActionMailer style email sending

To Install in Nuget Package : PM> install-package MvcMailer

Follow the simple example: Leave Request Mail

1) Define a mail model for the Leave Request :

    public class LeaveRequestMailModel
    {

       //main attributes
        public string Subject { get; set; }
        public string ToMail { get; set; }
        public string FromMail { get; set; }
        public string Message { get; set; }
        public string ViewName { get; set; }

       //customized attributes
        public string CustomEmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string FromDate { get; set; }
        public string ToDate { get; set; }
        public string LeaveType { get; set; }       
        public int LeaveApprovalHistoryId { get; set; }
        public int LeaveTransactionId { get; set; }
    }

2) Define Sending mail Service :


As you see here, populate method will call you back with an already instantiated MvcMailMessage object so you can set its properties as required.
MvcMailMessage is an extension of MailMessage from the core .Net library, with new properties added so you can specify the ViewName, MasterName, LinkedResources etc.

3) Design View for the Leave Request Email Template:

Here you can see a section of the View. In there I have  used inline css. Else email display without any styles.

4) Call Sending mail service via the controller :


Define the parameters relevant to the Leave request mail as in the above.

5) Setup the web config :


6) Finally you can run and see the output as follow:




It is really cool.  Those who are interested on this also better try and see.

Refer : https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
            http://smsohan.com/blog/2012/10/02/mvcmailer-new-api/ 

No comments:

Post a Comment