StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 2 · Configuration and Options

Typed Smtp Settings

easy100%1 of 1 solved

Problem statement

Bind the Smtp configuration section to a typed settings class with the options pattern, and give it to a service through its constructor.

Requirements

The SmtpSettings class, the EmailDispatcher methods and the SmtpController are already provided. Your task has two parts: bind the settings in SmtpSettingsApi/Program.cs, and write the constructor of EmailDispatcher in SmtpSettingsApi/Services/EmailDispatcher.cs.

With the options pattern, the app binds a configuration section to a class once. Any class that needs the values asks for IOptions<SmtpSettings> in its constructor, and Value holds the bound SmtpSettings.

appsettings.json already contains the section:

"Smtp": {
  "Host": "smtp.fabrikam-mail.net",
  "Port": 2525,
  "FromAddress": "noreply@fabrikam.io",
  "UseSsl": true,
  "TimeoutSeconds": 45
}

Program.cs

  • Bind the Smtp section (SmtpSettings.SectionName) to SmtpSettings with Configure, so IOptions<SmtpSettings> carries the values from the configuration. When a key in the section changes, the bound value changes with it

EmailDispatcher

  • The constructor receives IOptions<SmtpSettings> and keeps its Value in the settings field
  • The dispatcher reads its settings only from the injected options, never from IConfiguration

SmtpController (provided)

  • GET /smtp/settings answers 200 with the five settings
  • POST /email/preview answers 200 with a preview built from the settings. It answers 400 when to or subject is missing, or when to has no @
Sample Requests & Responses

GET /smtp/settings

Response (200 OK):

{
  "host": "smtp.fabrikam-mail.net",
  "port": 2525,
  "fromAddress": "noreply@fabrikam.io",
  "useSsl": true,
  "timeoutSeconds": 45
}

POST /email/preview

Request body:

{ "to": "ops@contoso.com", "subject": "March invoice run" }

Response (200 OK):

{
  "to": "ops@contoso.com",
  "subject": "March invoice run",
  "from": "noreply@fabrikam.io",
  "host": "smtp.fabrikam-mail.net",
  "port": 2525,
  "useSsl": true,
  "timeoutSeconds": 45,
  "envelope": "noreply@fabrikam.io -> ops@contoso.com via smtp.fabrikam-mail.net:2525 [ssl=on, timeout=45s]"
}

POST /email/preview with { "to": "ops", "subject": "x" }

Response (400 Bad Request):

{ "error": "The to address must contain an @ sign." }

More in Configuration and Options

See the module
Environment Override PrecedenceThis problem covers layering configuration from a base file, an environment specific file and environment variables, so each setting comes from the highest source that defines it.100%1 of 1 solvedeasyFail-Fast Options ValidationThis problem covers adding validation attributes to an options class and checking them as the application starts, so a bad setting stops it with an error naming that setting.100%1 of 1 solvedeasyPayment Gateway Settings ModuleThis problem covers layering the payment settings from files and environment variables, binding the nested gateway and retry sections to typed options, validating them at startup, and reading them in a service through IOptions.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.