code to configure (API_KEY) in appsetting.json & email.cs using C# web api
without using (ConfigurationManager.AppSettings[API_KEY]) in email.cs
code to configure (API_KEY) in appsetting.json & email.cs using C# web api
without using (ConfigurationManager.AppSettings[API_KEY]) in email.cs
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 25, 2024, 6:10 AM EST
To configure the MAILJET mail service in C#, you can follow these steps:
Install the Mailjet NuGet package in your project. You can do this by right-clicking on your project in Visual Studio, selecting "Manage NuGet Packages," and searching for "Mailjet" in the NuGet Package Manager.
Once the package is installed, you need to configure the API key in the appsettings.json file. Open the appsettings.json file and add the following section:
"MailjetSettings": { "ApiKey": "YOUR_API_KEY", "ApiSecret": "YOUR_API_SECRET" }Replace "YOUR_API_KEY" and "YOUR_API_SECRET" with your actual Mailjet API key and secret.
public class MailjetSettings { public string ApiKey { get; set; } public string ApiSecret { get; set; } }services.Configure(Configuration.GetSection("MailjetSettings"));Make sure you have the necessary using statements at the top of the file:
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection;private readonly MailjetSettings _mailjetSettings; public EmailService(IOptions mailjetSettings) { _mailjetSettings = mailjetSettings.Value; }Now you can access the API key and secret from the _mailjetSettings object in your email service methods.
Meghana MRank #8,429 · 222 reputationPosted Jan 25, 2024, 6:20 AM EST
I have declared in appsetting.json as below
"MailjetSettings": { "ApiKey": "YOUR_API_KEY", "ApiSecret": "YOUR_API_SECRET" }
In email.cs ,i'm using as below
string API_KEY = _configuration.GetValue(Constants.ApiKey);
It is throwing error for _configuration
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Mar 28, 2024, 1:20 AM EDT
Hello Meghna,
In the Constant.ApiKey need to double check.
public class Constants { public const string ApiKey = "MailjetSettings:ApiKey"; }And also check the configuration you have to resolve in the constructor side in your class.
Thanks