Friday, 6 October 2017

Send user Confirmation email after Registration with Activation Link in MVC4.

ASP.Net MVC: Send user Confirmation email after Registration with Activation Link
Description:


In this example we explain that how to send user Confirmation email after registration with activation link in Asp.Net using MVC razor view. Or how to send email confirmation to activate the account after successfully registration in MVC.or how to send an email with verification link to user after registration in MVC razor view. Or how to make confirmation link to activate accounts in MVC.or how to send user confirmation email after registration with activation link in MVC.
User Class:

using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class User
    {
        public int UserId { get; set; }

        [Required(ErrorMessage = "Required.")]
        public string Username { get; set; }

        [Required(ErrorMessage = "Required.")]
        public string Password { get; set; }

        [Required(ErrorMessage = "Required.")]
        [Compare("Password", ErrorMessage = "Passwords do not match.")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "Required.")]
        [EmailAddress(ErrorMessage = "Invalid email address.")]
        public string Email { get; set; }
      
        public System.DateTime CreatedDate { get; set; }
      
        public Nullable<System.DateTime> LastLoginDate { get; set; }
    }

Controller:

  public class EmployeeController : Controller
{
    [HttpPost]
    public ActionResult Index(User user)
    {
        UsersEntities usersEntities = new UsersEntities();
        usersEntities.Users.Add(user);
        usersEntities.SaveChanges();
        string message = string.Empty;
        switch (user.UserId)
        {
            case -1:
                message = "Username already exists.\\nPlease choose a different username.";
                break;
            case -2:
                message = "Supplied email address has already been used.";
                break;
            default:
                message = "Registration successful.\\nUser Id: " + user.UserId.ToString();
                SendActivationEmail(user);
                break;
        }
        ViewBag.Message = message;

        return View(user);
    }

    public ActionResult Activation()
    {
        ViewBag.Message = "Invalid Activation code.";
        if (RouteData.Values["id"] != null)
        {
            Guid activationCode = new Guid(RouteData.Values["id"].ToString());
            UsersEntities usersEntities = new UsersEntities();
            UserActivation userActivation = usersEntities.UserActivations.Where(p => p.ActivationCode == activationCode).FirstOrDefault();
            if (userActivation != null)
            {
                usersEntities.UserActivations.Remove(userActivation);
                usersEntities.SaveChanges();
                ViewBag.Message = "Activation successful.";
            }
        }

        return View();
    }

    private void SendActivationEmail(User user)
    {
        Guid activationCode = Guid.NewGuid();
        UsersEntities usersEntities = new UsersEntities();
        usersEntities.UserActivations.Add(new UserActivation
        {
            UserId = user.UserId,
            ActivationCode = activationCode
        });
        usersEntities.SaveChanges();

        using (MailMessage mm = new MailMessage("sender@gmail.com", user.Email)) // sender email required here
        {
            mm.Subject = "Account Activation to Login";
            string body = "Hello " + user.Username + ",";
            body += "<br /><br />Please click the following link to activate your account";
            body += "<br /><a href = '" + string.Format("{0}://{1}/Employee/Activation/{2}", Request.Url.Scheme, Request.Url.Authority, activationCode) + "'>Click here to activate your account.</a>";
            body += "<br /><br />Thanks";
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>"); // sender email and password required here
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
    }
View: 

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
  
    <title>ASP.Net MVC: Send user Confirmation email after Registration with Activation Link</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <div>
        <h1>@ViewBag.Message</h1>
    </div>
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment