In my ASP.NET Core project, I have the appsettings.json where I define the SmtpCredentials section. This section is a json array of configuration. Each service has to send an email with a specific SMTP credentials and servers.
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Hangfire": "Debug"
}
},
"AllowedHosts": "*",
"SmtpCredentials": [
{
"Name": "Default",
"MailFrom": "[email protected]",
"Username": "[email protected]",
"Password": "mypassword",
"Server": "smtp-mail.outlook.com",
"Port": 587,
"EnableSSL": true
},
{
"Name": "Learn",
"MailFrom": "[email protected]",
"Username": "[email protected]",
"Server": "smtp-mail.outlook.com",
"Port": 587,
"EnableSSL": true
}
],
"Api": {
"Endpoint": "https://localhost:44381"
}
}
My issue is to read the configuration in the Startup.cs and inject it.
services.Configure<SmtpSettings>(Configuration.GetSection("SmtpCredentials")
.Get<List<SmtpCredentialsSettings>>());
services.AddScoped(cfg => cfg.GetService<IOptions<SmtpSettings>>().Value);
In the injection, the settings are null. How can I read and inject this configuration?
Update
This is the SmtpSettings I refer to.
public class SmtpCredentialsSettings
{
public string Name { get; set; }
public string MailFrom { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Server { get; set; }
public int Port { get; set; }
public bool EnableSSL { get; set; }
}
public class SmtpSettings
{
public List<SmtpCredentialsSettings> SmtpCredentials { get; set; }
}
SmtpCredentialsSettingsclass?