Monday, 10 January 2022

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

1/10/2022 - By Pranav Singh 0

In this article i will tell you how to read or access email SMTP setting to send email present in Appsettings.json file in asp.net core 6 / MVC application in class library using c#.net. 

Other Articles: Access Connection String In Asp.Net Core 6 ClassLibrary From appsettings.json C# , appsettings.json Connection String Value Access in Asp.net Core ClassLibraryHow To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.NetAccess Connection String In Asp.Net Core 6 Controller From appsettings.json C#, appsettings.json Connection String Value Access in Asp.net Core ControllerAccess Connection String In DbContext File From appsettings.json C# In Asp.Net Core 6.

Now for this article first we will create a new asp.net core application and add the below mention app settings into it.

  "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 a class library project. In this class library we will add a new class file. We will make the added class file as static. Now add a function on which we will access the connection string in our class library.

But before writing the core we need to add the library reference of  Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder. For this we will right click on project and click on manager nuget package.  Add search for  Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder and install it.


Microsoft.Extensions.Configuration.Json

Microsoft.Extensions.Configuration.Json

Microsoft.Extensions.Configuration.Binder


Microsoft.Extensions.Configuration.Binder

After this we will write code to class library to read the email of smtp setting present in appsetting.json in class library. 

using Microsoft.Extensions.Configuration;

using System;

using System.IO; 

namespace ClassLibrary1

{

    public static class Class1

    {

        public static void EmaiSettings()

        {

            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");

        }

    }

}

 


Now we have done run the application and check the output.


How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Class Library 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