What you probably want to achieve is like this:
File: appsettings.json
(where you define your data)
{
...
"EmailSettings": {
"From": "no-reply@example.com",
"Username": "username",
"Password": "password",
"Host": "smtp.example.com",
"Port": 25
},
...
}
File: Email.cs
(where you define a model that will contain the data)
public class EmailSettings
{
public string From { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Host { get; set; }
public int Port { get; set; }
}
File: Startup.cs
(where you bind the data to the model that will be available everywhere)
public void ConfigureServices(IServiceCollection services)
{
...
services.AddConfiguration<EmailSettings>(Configuration, "EmailSettings");
...
}
File: Email.cs
(where you define how to send a mail)
public class EmailService
{
private readonly EmailSettings _mailSettings;
public EmailService(EmailSettings mailSettings)
{
_mailSettings = mailSettings;
}
public async Task<bool> SendMail(MailRequest mailRequest)
{
//Here goes your code to send a message using "_mailSettings" like:
// _mailSettings.From
// _mailSettings.Port
//ect...
}
}
To do so open this tutorial and go to Way 6 named "#6 AppSettings – PRE-Binding"
There your will find different ways to make this work but the 6th way is highly recommended