Mail Server Settings in AppSettings.json file
The following Mail Server settings need to be saved in the
AppSettings.json file.
{
"Smtp": {
"Server": "smtp.gmail.com",
"Port": 587,
"EnableSsl": true,
"DefaultCredentials": false,
"FromAddress": "sender@gmail.com",
"UserName": "sender@gmail.com",
"Password": "GMAILor2STEP-PASSWORD"
}
}
Model
The Model class consists of the following properties.
public class EmailModel
{
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
Namespaces
You will need to import the following namespaces.
using System.Net;
using System.Net.Mail;
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
Inside this Action method handles the call made from the POST function from the View.
When the Form is submitted, the posted values are captured through the EmailModel class object. All the fetched values are set into an object of the MailMessage class.
Then an object of the
SmtpClient class is created, where the settings of the Mail Server are read from the
AppSettings.json file using
IConfiguration interface.
Finally, the email is being sent using Send method of SmtpClient class object and a success message is display in View.
public class HomeController : Controller
{
private IConfiguration Configuration;
public HomeController(IConfiguration _configuration)
{
this.Configuration = _configuration;
}
//Get: Home
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(EmailModel model)
{
//Read SMTP settings from AppSettings.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 fromAddress = this.Configuration.GetValue<string>("Smtp:FromAddress");
string userName = this.Configuration.GetValue<string>("Smtp:UserName");
string password = this.Configuration.GetValue<string>("Smtp:Password");
using (MailMessage mm = new MailMessage(fromAddress, model.To))
{
mm.Subject = model.Subject;
mm.Body = model.Body;
mm.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = host;
smtp.Port = port;
smtp.EnableSsl = enableSsl;
smtp.UseDefaultCredentials = defaultCredentials;
smtp.Credentials = new NetworkCredential(userName, password);
smtp.Send(mm);
}
}
return View();
}
}
View
HTML Markup
The View consists of an
HTML Form with following
ASP.Net Tag Helpers attributes and a Submit Button.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
When the Send Button is clicked, the Form gets submitted and the Model object is sent to the Controller.
Finally, the
ViewBag object is checked for NULL and if it is not NULL then the value of the object is displayed using
JavaScript Alert Message Box.
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@model AppSettings_Email_MVC_Core.Models.EmailModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 80px">To:</td>
<td><input type="text" name="To" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="Subject" /></td>
</tr>
<tr>
<td valign="top">Body:</td>
<td><textarea cols="20" rows="10" name="Body"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Send" /></td>
</tr>
</table>
</form>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
</body>
</html>
Errors while sending Email using Gmail
The following error occurs when you try to send email using
Gmail credentials in your ASP.Net MVC application.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Solution
Screenshots
Email Form
Received Email
Downloads