Sunday, 9 January 2022

How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.Net

1/09/2022 - By Pranav Singh 0

In this article i will show you how you can access or read the email setting from Appsettings.json file in asp.net core 6 / MVC application using c#.net. So for this first we will create a new asp.net core web application and add open the Appsettings.json file. In Appsettings.json file we will first add the email server setting as shown below.


Other Articles: Retrieve Connection String From Web.config In ASP.net Using C#.Net (AppSettings,ConnectionStrings), Access Connection String In Asp.Net Core 6 Controller From appsettings.json C# ,  appsettings.json Connection String Value Access in Asp.net Core Controller, Access Connection String In Asp.Net Core 6 ClassLibrary From appsettings.json C# | appsettings.json Connection String Value Access in Asp.net Core ClassLibrary, Access Connection String In DbContext File From appsettings.json C# In Asp.Net Core 6.


"Smtp": {

    "Host": "smtp.xxxx.com",

    "EnableSsl": true,

    "UserName": "admin@xxxx.com",

    "FromEmail": "admin@xxxxx.com",

    "Password": "iNxxxx123",

    "DefaultCredentials": false,

    "Port": 123

  }

 

After this we will add the below code into the controller to access the email server setting or SMTP server setting from appsetting.json file.


  var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false);

            IConfiguration config = builder.Build();

            string host = config.GetValue<string>("Smtp:Host");

            string username = config.GetValue<string>("Smtp:UserName");

            string fromemailid = config.GetValue<string>("Smtp:FromEmail");

            string password = config.GetValue<string>("Smtp:Password");

            string defaultcredentials = config.GetValue<string>("Smtp:DefaultCredentials");

            string port = config.GetValue<string>("Smtp:Port");

            string enablessl = config.GetValue<string>("Smtp:EnableSsl");

 In 

In above code i have used ConfigurationBuilder to read the appsettings.json file. The name space for ConfigurationBuilder is Microsoft.Extensions.Configuration. Now we will add the code to display it on controller view. 


      public IActionResult Index()

        {

            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false);

            IConfiguration config = builder.Build();

            string host = config.GetValue<string>("Smtp:Host");

            string username = config.GetValue<string>("Smtp:UserName");

            string fromemailid = config.GetValue<string>("Smtp:FromEmail");

            string password = config.GetValue<string>("Smtp:Password");

            string defaultcredentials = config.GetValue<string>("Smtp:DefaultCredentials");

            string port = config.GetValue<string>("Smtp:Port");

            string enablessl = config.GetValue<string>("Smtp:EnableSsl");

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("Host: " + host + "<br/>");

            stringBuilder.Append("UserName: " + username + "<br/>");

            stringBuilder.Append("FromEmail: " + fromemailid + "<br/>");

            stringBuilder.Append("Password: " + password + "<br/>");

            stringBuilder.Append("DefaultCredentials: " + defaultcredentials + "<br/>");

            stringBuilder.Append("Port: " + port + "<br/>");

            stringBuilder.Append("EnableSsl: " + enablessl + "<br/>");

            ViewBag.Data = stringBuilder.ToString();

            return View();

        }

 

Now lets write code to display the accessed configuration setting in controller view.


@{

    ViewData["Title"] = "Home Page";

}

<div>

    @ViewBag.Data

</div>

 

Now we have done run the code and check the output. But before checking the final output lets put the break point and check whether we are able to access the detail or not.  so here we can see we are able to access the detail.


How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.Net


Now lets check the output.

How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.Net

Download

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top