MVC Configure to send verification email after registeration

MVC 5 could be configured to send verification mail, and I suggest it!
To do this you need to configure the EMailService, there are many many example that use SendGrid but none if you wanna use your own smtp server.

go to App_start\IdendityConfig.cs and modify the class EMailService like this:

public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        var smtp = new System.Net.Mail.SmtpClient();
        var mail = new System.Net.Mail.MailMessage();

        mail.IsBodyHtml = true;
        mail.From = new System.Net.Mail.MailAddress("prova@hln.it", "Prova Mail");
        mail.To.Add(message.Destination);
        mail.Subject = message.Subject;
        mail.Body = message.Body;

        smtp.Timeout = 1000;

        var t = Task.Run(() => smtp.SendAsync(mail, null));

        return t;

        // Plug in your email service here to send an email.
        //return Task.FromResult(0);
    }
}

go to AccountController.cs and uncomment the lines that Visual studio will generated for us and obtain this result
Here you can configure your e-mail message using XSLT or other you prefer.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            return RedirectToAction("Index", "Home");
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

Now when a user register on your site a mail is sent to it with a confirmation link.

Annunci

Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in .Net, c#, MVC e contrassegnata con , , , . Contrassegna il permalink.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...