HiKao,
Check this example. Now please take its reference and correct your code.
Template
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<img src="https://www.aspsnippets.com/images/Blue/Logo.png" /><br />
<br />
<div style="border-top: 3px solid #22BCE5">
</div>
<span style="font-family: Arial; font-size: 10pt">Hello <b>{UserName}</b>,<br />
<br />
A new article has been published on ASPSnippets.<br />
<br />
<a style="color: #22BCE5" href="{Url}">{Title}</a><br />
{Description}
<br />
<br />
Thanks<br />
ASPSnippets </span>
</body>
</html>
Model
using System.ComponentModel;
public class Email
{
public string Subject { get; set; }
[DisplayName("User Name")]
public string UserName { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public string Recipient { get; set; }
}
Namespaces
using System.Net.Mail;
using System.IO;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult SendEmail(Email email)
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", email.UserName);
body = body.Replace("{Title}", email.Title);
body = body.Replace("{Url}", email.Url);
body = body.Replace("{Description}", email.Description);
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(email.UserName);
mailMessage.Subject = email.Subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(email.Recipient));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = email.UserName;
NetworkCred.Password = "password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage);
}
return View();
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_530061_PlaceHolder_Email_Body.Models.Email>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<% using (Html.BeginForm("SendEmail", "Home", FormMethod.Post))
{%>
<table>
<tr>
<td>
<%: Html.LabelFor(model => model.Subject) %>
</td>
<td>
<%: Html.TextAreaFor(model => model.Subject)%>
</td>
</tr>
<tr>
<td>
<%: Html.LabelFor(model => model.UserName) %>
</td>
<td>
<%:Html.DropDownListFor(m => m.UserName,
new List<SelectListItem>{
new SelectListItem{Text="sender@gmail.com", Value="sender@gmail.com"},
new SelectListItem{Text="sender2@gmail.com", Value="sender2@gmail.com"}
}, "Please select")%>
</td>
</tr>
<tr>
<td>
<%: Html.LabelFor(model => model.Title) %>
</td>
<td>
<%: Html.TextBoxFor(model => model.Title) %>
</td>
</tr>
<tr>
<td>
<%: Html.LabelFor(model => model.Url) %>
</td>
<td>
<%: Html.TextAreaFor(model => model.Url)%>
</td>
</tr>
<tr>
<td>
<%: Html.LabelFor(model => model.Description) %>
</td>
<td>
<%: Html.TextAreaFor(model => model.Description) %>
</td>
</tr>
<tr>
<td>
<%: Html.LabelFor(model => model.Recipient) %>
</td>
<td>
<%: Html.TextBoxFor(model => model.Recipient) %>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Send Email" />
</td>
<td>
</td>
</tr>
</table>
<% } %>
</body>
</html>
Screenshots
Web Form

Html Body

Value in Controller
