From 0b1037819a297e363e588a9b2223c54b732f8b9c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 23 Oct 2024 17:40:50 +0100 Subject: [PATCH] fix: use SSL/TLS not STARTTLS for outbound mail --- OliverBooth/Controllers/ContactController.cs | 35 ++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/OliverBooth/Controllers/ContactController.cs b/OliverBooth/Controllers/ContactController.cs index e821484..bf38e52 100644 --- a/OliverBooth/Controllers/ContactController.cs +++ b/OliverBooth/Controllers/ContactController.cs @@ -1,6 +1,9 @@ -using MailKitSimplified.Sender.Services; +using System.Net.Mail; +using MailKit.Security; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; +using MimeKit; +using SmtpClient = MailKit.Net.Smtp.SmtpClient; namespace OliverBooth.Controllers; @@ -56,16 +59,20 @@ public class ContactController : Controller StringValues subject = form["subject"]; StringValues message = form["message"]; - await using SmtpSender sender = CreateSender(); + using SmtpClient client = CreateSmtpClient(out string destination); try { - await sender.WriteEmail - .To("Oliver Booth", _destination.Get()) - .From("Contact via Website", _sender.Get()) - .ReplyTo(name, email) - .Subject(subject) - .BodyText(message) - .SendAsync(); + var emailAddress = email.ToString(); + var mailMessage = new MailMessage(); + + mailMessage.From = new MailAddress(emailAddress, name); + mailMessage.ReplyToList.Add(new MailAddress(emailAddress, name)); + mailMessage.To.Add(destination); + mailMessage.Subject = subject; + mailMessage.Body = message; + mailMessage.IsBodyHtml = false; + + await client.SendAsync(MimeMessage.CreateFromMailMessage(mailMessage)); } catch (Exception e) { @@ -78,16 +85,18 @@ public class ContactController : Controller return RedirectToPage("/Contact/Result"); } - private SmtpSender CreateSender() + private SmtpClient CreateSmtpClient(out string destination) { IConfigurationSection mailSection = _configuration.GetSection("Mail"); string? mailServer = mailSection.GetSection("Server").Value; string? mailUsername = mailSection.GetSection("Username").Value; string? mailPassword = mailSection.GetSection("Password").Value; ushort port = mailSection.GetSection("Port").Get(); + destination = mailSection.GetSection("Destination").Value ?? string.Empty; - var sender = SmtpSender.Create(mailServer, port); - sender.SetCredential(mailUsername, mailPassword); - return sender; + var client = new SmtpClient(); + client.Connect(mailServer, port, SecureSocketOptions.SslOnConnect); + client.Authenticate(mailUsername, mailPassword); + return client; } }