Picflight
Picflight

Reputation: 3852

How to send email address verification email in MVC?

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.

Upvotes: 1

Views: 1811

Answers (2)

Paul
Paul

Reputation: 36319

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/

Upvotes: 1

Hal
Hal

Reputation: 1264

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

Upvotes: 1

Related Questions