In this article I will explain with an example, how to read Email Settings from AppSettings.json in ASP.Net Core.
The SMTP Mail settings are stored in the Smtp section of the AppSettings.json file.
The SMTP Mail settings will be read using the IConfiguration interface from the AppSettings.json file in ASP.Net Core.
 
 
Adding the AppSettings.json file
In order to add AppSettings.json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button.
Read Email Settings from AppSettings.json in ASP.Net Core
 
Once the File is created, it will have a DefaultConnection, below that a new Smtp section will be added.
{
 "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
 },
 "Smtp": {
    "Server": "smtp.gmail.com",
    "Port": 587,
    "FromAddress": "sender@gmail.com",
    "UserName": "sender@gmail.com",
    "Password": "GMAILor2STEP-PASSWORD"
 }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.Net;
using System.Net.Mail;
using Microsoft.Extensions.Configuration;
 
 
Model
Following is a Model class named EmailModel with the following properties.
public class EmailModel
{
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method handles the call made from the POST function from the View.
Note: This example uses Model class object for capturing Form field values, for more details please refer my article ASP.Net MVC: Form Submit (Post) example.
 
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.
Note: For more information about the IConfiguration interface, please refer Using IConfiguration in ASP.Net Core.
Here Gmail is the Mail Server hence the Mail Settings of the Gmail SMTP Server are used.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be same as the Gmail Username specified in credentials.
 
public class HomeController : Controller
{
    private IConfiguration Configuration;
    public HomeController(IConfiguration _configuration)
    {
        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");
        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.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(userName, password);
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = port;
                smtp.Send(mm);
            }
        }
 
        return View();
    }
}
 
 
View
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.
The Form consists of HTML TextBox, TextArea and a Submit Button.
When the Send Button is clicked, the Form gets submitted and the Model object is sent to the Controller.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    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>
</body>
</html>
 
 
Screenshots
Email Form
Read Email Settings from AppSettings.json in ASP.Net Core
 
Received Email
Read Email Settings from AppSettings.json in ASP.Net Core
 
 
Downloads