From ee8a2cb569e7affb3851e8ad55fd3c286606d540 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 20 Oct 2024 04:02:54 +0100 Subject: [PATCH] feat: add configurable SMTP settings --- OliverBooth/Controllers/ContactController.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/OliverBooth/Controllers/ContactController.cs b/OliverBooth/Controllers/ContactController.cs index 5dc16e2..b1bb673 100644 --- a/OliverBooth/Controllers/ContactController.cs +++ b/OliverBooth/Controllers/ContactController.cs @@ -11,6 +11,7 @@ public class ContactController : Controller private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly IConfigurationSection _destination; + private readonly IConfigurationSection _sender; /// /// Initializes a new instance of the class. @@ -21,7 +22,10 @@ public class ContactController : Controller { _logger = logger; _configuration = configuration; - _destination = configuration.GetSection("Mail").GetSection("Destination"); + + IConfigurationSection mailConfiguration = configuration.GetSection("Mail"); + _destination = mailConfiguration.GetSection("Destination"); + _sender = mailConfiguration.GetSection("Sender"); } [HttpGet("{_?}")] @@ -57,7 +61,8 @@ public class ContactController : Controller { await sender.WriteEmail .To("Oliver Booth", _destination.Get()) - .From(name, email) + .From(name, _sender.Get()) + .ReplyTo(email) .Subject($"[Contact via Website] {subject}") .BodyText(message) .SendAsync(); @@ -79,8 +84,9 @@ public class ContactController : Controller string? mailServer = mailSection.GetSection("Server").Value; string? mailUsername = mailSection.GetSection("Username").Value; string? mailPassword = mailSection.GetSection("Password").Value; + ushort port = mailSection.GetSection("Port").Get(); - var sender = SmtpSender.Create(mailServer); + var sender = SmtpSender.Create(mailServer, port); sender.SetCredential(mailUsername, mailPassword); return sender; }