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 ClassLibrary, How To Read / Access Email Setting In Appsettings.json In Asp.Net Core 6 in Controller Using C#.Net, 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 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.Binder
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.
0 comments:
Please let me know your view