In web forms I used MailDefinition class to send email templates. How can I do the same with MVC?
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus =
MembershipService.CreateUser(model.UserName, model.Password, model.Email);
if (createStatus == MembershipCreateStatus.Success)
{
// TODO: Send email verification code go here?
//FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
// If we got this far, something failed, redisplay form
return View(model);
}
I am looking for a solution that conforms with MVC design pattern, not sure how to go about it.
imo, the best way to do this is to solve by composition, rather than inheritance.
I usually have an INotifier interface that I use which has a Notify method that takes in a user, a title, and a body. I then can iether manually set that INotfier to my EmailNotifier implementation, or else pass it in via the constructor or via the IoC container.
In any case, my EmailNotifier implementation then extracts the user’s email from the body, and uses the System.Net.Mail namespace mail objects to send an email. this has the added benefit that System.Net.Mail has alreayd figured out for you how to get all the email settings out of your config file, so you have one place to set/ update the settings.
I also normally have a NullNotifier that implements INotifier but does nothing, so my controllers can safely call notifier.Notify(user, title, body) w/o worrying about NRE’s.
here’s more info on System.Net.Mail: http://www.systemnetmail.com/