views:

111

answers:

2

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.

+1  A: 

I create a base controller that has a virtual method sendEmail(MyBaseViewModel model) that can be overridden on inherited controllers, but has logic that looks for an Email, FromEmail, and Subject in the view model and if found sets a sendMail boolean to true and also looks for an AlternateEmailView string in the viewmodel.

I then have a custom view result that looks for the trueness of the sendMail property and then actually renders the view (or alternate view) and sends the email, then renders the normal view to the browser. Seems to work great and allows me to treat my email content as simply another view to maintain.

I should add that I also have a convention in my view names so that if I don't provide the AlternateEmailView property, it looks for ViewName_Email, then sends the normal view if not found.

Hal

Hal
+1  A: 

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/

Paul
Paul, thanks for the response. I am so new to MVC that most of the stuff you mention just flew right by me. I am looking at open source code (SutekiShop) to figure what the correct way of implementing things in MVC. Do you think what you mention can be found on Suteki shop? Looking at code is easier for me to follow and understand.
Picflight
Never heard of SutekiShop, sorry. What I was talking about isn't MVC specific, either, it's just regular OOP + inversion of control.
Paul