Hi lingers,
For sending email with registration details i have create a html template.
That html template contains some placeholder which will be replaced with actual value while sending email.
For more details sending email refer below articles.
ASP.Net Core: Send Email with example
Send Email with HTML Templates using MailKit in ASP.Net Core
Using the article i have create the sample.
Database
CREATE TABLE UserDetails
(
UserID VARCHAR(50),
Password VARCHAR(50),
Name VARCHAR(50),
MobNumber BIGINT,
EmailID VARCHAR(50),
EmpID VARCHAR(50),
RoleID VARCHAR(50),
DesignID VARCHAR(50),
LastLoggedDate DATETIME,
CreatedBy VARCHAR(50)
)
Appsettings.json
"Smtp": {
"Server": "smtp.gmail.com",
"Port": 587,
"EnableSsl": true,
"DefaultCredentials": true,
"From": "sender@gmail.com",
"Username": "sender@gmail.com",
"Password": "password"
}
Model
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EF_Core_7_MVC.Models
{
[Table("UserDetails")]
public class UserDetail
{
[Key]
public string? UserID { get; set; }
public string? Password { get; set; }
public string? Name { get; set; }
public Int64 MobNumber { get; set; }
public string? EmailID { get; set; }
public string? EmpID { get; set; }
public string? RoleID { get; set; }
public string? DesignID { get; set; }
public DateTime? LastLoggedDate { get; set; }
public string? CreatedBy { get; set; }
}
}
Interface
using EF_Core_7_MVC.Models;
namespace EF_Core_7_MVC.Abstract
{
public interface IUserDetailManagement
{
string InsertUserDatail(UserDetail usr);
}
}
Repository Class
using EF_Core_7_MVC.Abstract;
using EF_Core_7_MVC.Models;
namespace EF_Core_7_MVC.Repository
{
public class UserDetailDataManagement : IUserDetailManagement
{
UserDetailDbContext _context = new UserDetailDbContext();
public string InsertUserDatail(UserDetail usr)
{
string msg = string.Empty;
try
{
usr.CreatedBy = usr.Name;
usr.LastLoggedDate = DateTime.Now;
_context.UserDetails.Add(usr);
_context.SaveChanges();
msg = "success";
}
catch (Exception ex)
{
msg = "failed";
}
return msg;
}
}
}
DBContext
namespace EF_Core_7_MVC
{
public class UserDetailDbContext : DbContext
{
public DbSet<UserDetail> UserDetails { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=.\\SQL2022;Initial Catalog=AjaxSamples;uid=sa;pwd=pass@123;Trusted_Connection=True;TrustServerCertificate=true;");
}
base.OnConfiguring(optionsBuilder);
}
}
}
HTML Template
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
body { font-family: Arial; font-size: 10pt; }
</style>
</head>
<body>
<span>
Hello <b>{UserName}</b>,<br /><br />
Your registration details.<br /><br />
</span>
<table>
<tr>
<td>User ID</td>
<td>{UserID}</td>
</tr>
<tr>
<td>Password</td>
<td>{Password}</td>
</tr>
<tr>
<td>Name</td>
<td>{Name}</td>
</tr>
<tr>
<td>Mobile Number</td>
<td>{MobNumber}</td>
</tr>
<tr>
<td>Email</td>
<td>{EmailID}</td>
</tr>
<tr>
<td>Employee ID</td>
<td>{EmpID}</td>
</tr>
<tr>
<td>Role</td>
<td>{RoleID}</td>
</tr>
<tr>
<td>Designation</td>
<td>{DesignID}</td>
</tr>
</table>
</body>
</html>
Controller
using EF_Core_7_MVC.Models;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mail;
using System.Net;
namespace EF_Core_7_MVC.Controllers
{
public class HomeController : Controller
{
private IWebHostEnvironment Environment { get; set; }
public IConfiguration Configuration { get; set; }
public HomeController(IConfiguration configuration, IWebHostEnvironment environment)
{
this.Configuration = configuration;
this.Environment = environment;
}
EF_Core_7_MVC.Repository.UserDetailDataManagement objBL = new EF_Core_7_MVC.Repository.UserDetailDataManagement();
public ActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(UserDetail usr)
{
if (ModelState.IsValid)
{
string msg = objBL.InsertUserDatail(usr);
if (msg == "success")
{
// Sending Email.
//Read SMTP section from AppSetting.json.
string host = this.Configuration.GetValue<string>("Smtp:Server");
int port = this.Configuration.GetValue<int>("Smtp:Port");
bool enableSsl = this.Configuration.GetValue<bool>("Smtp:EnableSsl");
bool defaultCredentials = this.Configuration.GetValue<bool>("Smtp:DefaultCredentials");
string from = this.Configuration.GetValue<string>("Smtp:From");
string userName = this.Configuration.GetValue<string>("Smtp:Username");
string password = this.Configuration.GetValue<string>("Smtp:Password");
using (MailMessage mm = new MailMessage(from, usr.EmailID))
{
mm.Subject = "Registration";
mm.Body = this.PopulateBody(usr);
mm.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = host;
smtp.EnableSsl = enableSsl;
NetworkCredential networkCred = new NetworkCredential(userName, password);
smtp.UseDefaultCredentials = defaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = port;
smtp.Send(mm);
}
}
return RedirectToAction("Index");
}
else
{
ViewBag.ErrorInfo = "Data not saved, try again latter";
}
}
return RedirectToAction("Index");
}
private string PopulateBody(UserDetail userDetail)
{
string body = string.Empty;
string path = Path.Combine(this.Environment.WebRootPath, "Template\\EmailTemplate.html");
using (StreamReader reader = new StreamReader(path))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userDetail.Name);
body = body.Replace("{UserID}", userDetail.UserID);
body = body.Replace("{Password}", userDetail.Password);
body = body.Replace("{Name}", userDetail.Name);
body = body.Replace("{MobNumber}", userDetail.MobNumber.ToString());
body = body.Replace("{EmailID}", userDetail.EmailID);
body = body.Replace("{EmpID}", userDetail.EmpID);
body = body.Replace("{RoleID}", userDetail.RoleID);
body = body.Replace("{DesignID}", userDetail.DesignID);
return body;
}
}
}
View
@model EF_Core_7_MVC.Models.UserDetail
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
<h4>UserDetail</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserID" class="control-label"></label>
<input asp-for="UserID" class="form-control" />
<span asp-validation-for="UserID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MobNumber" class="control-label"></label>
<input asp-for="MobNumber" class="form-control" />
<span asp-validation-for="MobNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmailID" class="control-label"></label>
<input asp-for="EmailID" class="form-control" />
<span asp-validation-for="EmailID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmpID" class="control-label"></label>
<input asp-for="EmpID" class="form-control" />
<span asp-validation-for="EmpID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="RoleID" class="control-label"></label>
<input asp-for="RoleID" class="form-control" />
<span asp-validation-for="RoleID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DesignID" class="control-label"></label>
<input asp-for="DesignID" class="form-control" />
<span asp-validation-for="DesignID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="SUBMIT" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
</body>
</html>