Sending Email in ASP MVC with Mvc Mailer

This is a very simple and good email API that we can use in asp mvc app. It allow us to have something below ( i got it from MVC Mailer site )

  • Write clean code to send emails instead of spaghetti code
  • Reuse the power of master pages, views and data
  • Easily write unit test for email sending code
  • Send multi-part emails
  • Do some or all the above painlessly.

Now shall we start to implement it?

  • Create a new ASP  app with VS 2013.
  • Search and Add MvcMailer nuget from Nuget Manager or command :

  • Create a new folder which called “Mailer” which contains 3 files :
  • ExampleEmailModel.cs

  • IExampleMailer.cs

  •  ExampleMailer.cs

  •  Create a sub-folder in View folder which called “ExampleMailer”. It will contain 3 files:
  • _Layout.cshtml

  •  Template1.cshtml

  •  Template2.cshtml

  •  After that , we open the HomeController and add these line of codes into Index action

  •  Please remember to declare the ExampleMailer class first , in the HomeController

  •  Final thing need to be take care of is the email setting in web.config . Please take a look at <> <mailSetting> . We need to put in our email address , server name , username and password.  For example :

That’s all .

Code Sample Download