Today, in this article, I will explain how to send an activation link on the email id after user registration using MVC and Web API. In real time, often, we see that after registration by the user, it will send a code to the registered email id and when we click this link, then it will activate our email id. Actually, what happens behind this is when we register our details, the system will send an activation code on email id and also saves the same link or code in our database and when we activate our email id, then it will check and match that with our database entry. If matched, it will activate the profile, otherwise it doesn't.
Step 1
We need to create two tables - one for RegistrationDetails and a second for Department.


Step2
Now, we need to create an MVC application but we will create two projects here - First for Web API project for the creation of service and the second is MVC project for consuming that service. So now, let us add the first MVC project.
Open Visual Studio and go to File->New ->Web application ->select MVC ->OK.

Step 3
Now, add tables in Web API project using Entity Framework. For this, go to Models folder ->right-click -> Add -> New item -> ADO.NET Entity Data Model -> click Add -> select database first approach->click Next.
Select "New Connection" and give the connection details, then select database -> click OK.
Choose tables and click OK.

Step 4
Now, we will write the logic for binding the department so, I create a folder, Business Logic, and take a class RegLogic.cs to write the logic for binding.
But before that, we add a folder in the Models folder. l will create a partial class of RegDetail.cs and add some properties for finding the address of local URL.
- namespace EmailActivationAPI.Models
- {
- public partial class RegDetail
- {
- public string scheme { get; set; }
- public string host { get; set; }
- public string port { get; set; }
- }
- }
After that, we will write the logic.
- public List<Department> BindDept()
- {
- EmployeeDBEntities objEntity = new EmployeeDBEntities();
- return objEntity.Departments.ToList();
- }
Again, we will write logic for inserting the user details in RegLogic.cs class.
- public string RegisterUser(RegDetail objReg)
- {
- objReg.EmailIVeryFied = false;
- objReg.ActivateionCode = Guid.NewGuid();
- EmployeeDBEntities objEntity = new EmployeeDBEntities();
- objEntity.RegDetails.Add(objReg);
- int i = objEntity.SaveChanges();
- if(i > 0)
- {
- SendVerificationLinkEmail(objReg.EmailId, objReg.ActivateionCode.ToString(), objReg.scheme, objReg.host, objReg.port);
- return "Registration has been done,And Account activation link" + "has been sent your eamil id:" + objReg.EmailId;
- }
- else
- {
- return "Registration has been Faild";
- }
- }
After that, we will write code for sending the activation code on email id.
- private void SendVerificationLinkEmail(string emailId, string activationcode, string scheme, string host, string port)
- {
- var varifyUrl = scheme + "://" + host + ":" + port + "/JobSeeker/ActivateAccount/" + activationcode;
- var fromMail = new MailAddress("your email id", "welcome mithilesh");
- var toMail = new MailAddress(emailId);
- var frontEmailPassowrd = "your password";
- string subject = "Your account is successfull created";
- string body = "<br/><br/>We are excited to tell you that your account is" +
- " successfully created. Please click on the below link to verify your account" +
- " <br/><br/><a href='" + varifyUrl + "'>" + varifyUrl + "</a> ";
- var smtp = new SmtpClient
- {
- Host = "smtp.gmail.com",
- Port = 587,
- EnableSsl = true,
- DeliveryMethod = SmtpDeliveryMethod.Network,
- UseDefaultCredentials = false,
- Credentials = new NetworkCredential(fromMail.Address, frontEmailPassowrd)
- };
- using (var message = new MailMessage(fromMail, toMail)
- {
- Subject = subject,
- Body = body,
- IsBodyHtml = true
- })
- smtp.Send(message);
- }
Step5
After that, we will add an API Controller in Web API project.

Now, we can see the our Web API project architecture.

Now, we have to write the WebAPI code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using EmailActivationAPI.Models;
- using EmailActivationAPI.BusinessLogic;
- namespace EmailActivationAPI.Controllers
- {
- [RoutePrefix("api/UserReg")]
- public class RegAPIController : ApiController
- {
- RegLogic objReg = new RegLogic();
- [HttpPost]
- [Route("RegUser")]
- public string RegisterUser(RegDetail data)
- {
- string strObj;
- try
- {
- strObj = objReg.RegisterUser(data);
- }
- catch (ApplicationException ex)
- {
- throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = ex.Message });
- }
- catch (Exception ex)
- {
- throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadGateway, ReasonPhrase = ex.Message });
- }
- return strObj;
- }
- [HttpGet]
- [Route("BindDepartment")]
- public List<Department> DeptBind()
- {
- List<Department> objDept = new List<Department>();
- try
- {
- objDept = objReg.BindDept();
- }
- catch (ApplicationException ex)
- {
- throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = ex.Message });
- }
- catch (Exception ex)
- {
- throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadGateway, ReasonPhrase = ex.Message });
- }
- return objDept;
- }
- }
- }








Raichand RayPosted Sep 16, 2021, 11:42 AM
Hi, Please Provide Project Code. It helps a newbie to learn easily.
Prajwal prabhakarPosted Sep 19, 2019, 5:18 AM
Can you explain how the obtained link can be redirected to another page
Tabarak HussainPosted Jun 12, 2019, 3:23 AM
Thanks, Nice Explanation...........And this is help full
Mithilesh KumarPosted Jun 12, 2019, 2:22 AM
Follow the next article related this article .......https://www.c-sharpcorner.com/article/how-to-activate-link-after-sending-email-of-activation-link-by-user-in-mvc-and-w2/
Praveen PPosted Nov 30, 2018, 12:10 AM
Can you provide download link for this project
hye jinPosted Jun 1, 2018, 2:09 AM
I am doing my final year project and am assigned to do registration page with email verification, forgot password page . currently, I am using ASP.NET CORE 1.0 , and using entity framework classes.is there any other way to do the registration page without creating web api project ?? I am so stressed out now T_T. If it's possible , could you please share me some link dealing with registration ?? Thank you in advance.
sumit singhPosted May 18, 2018, 4:47 AM
But there is a different code in asp.net core
sumit singhPosted May 18, 2018, 2:18 AM
Can you do this in asp.net core web api
Mithilesh KumarPosted May 5, 2018, 3:45 AM
Next Article Link https://www.c-sharpcorner.com/article/how-to-activate-link-after-sending-email-of-activation-link-by-user-in-mvc-and-w2/
Farhan AhmedPosted May 2, 2018, 11:15 PM
Nice explanation.....