fix: use SSL/TLS not STARTTLS for outbound mail
This commit is contained in:
parent
af09e29d17
commit
0b1037819a
@ -1,6 +1,9 @@
|
|||||||
using MailKitSimplified.Sender.Services;
|
using System.Net.Mail;
|
||||||
|
using MailKit.Security;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using MimeKit;
|
||||||
|
using SmtpClient = MailKit.Net.Smtp.SmtpClient;
|
||||||
|
|
||||||
namespace OliverBooth.Controllers;
|
namespace OliverBooth.Controllers;
|
||||||
|
|
||||||
@ -56,16 +59,20 @@ public class ContactController : Controller
|
|||||||
StringValues subject = form["subject"];
|
StringValues subject = form["subject"];
|
||||||
StringValues message = form["message"];
|
StringValues message = form["message"];
|
||||||
|
|
||||||
await using SmtpSender sender = CreateSender();
|
using SmtpClient client = CreateSmtpClient(out string destination);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await sender.WriteEmail
|
var emailAddress = email.ToString();
|
||||||
.To("Oliver Booth", _destination.Get<string>())
|
var mailMessage = new MailMessage();
|
||||||
.From("Contact via Website", _sender.Get<string>())
|
|
||||||
.ReplyTo(name, email)
|
mailMessage.From = new MailAddress(emailAddress, name);
|
||||||
.Subject(subject)
|
mailMessage.ReplyToList.Add(new MailAddress(emailAddress, name));
|
||||||
.BodyText(message)
|
mailMessage.To.Add(destination);
|
||||||
.SendAsync();
|
mailMessage.Subject = subject;
|
||||||
|
mailMessage.Body = message;
|
||||||
|
mailMessage.IsBodyHtml = false;
|
||||||
|
|
||||||
|
await client.SendAsync(MimeMessage.CreateFromMailMessage(mailMessage));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -78,16 +85,18 @@ public class ContactController : Controller
|
|||||||
return RedirectToPage("/Contact/Result");
|
return RedirectToPage("/Contact/Result");
|
||||||
}
|
}
|
||||||
|
|
||||||
private SmtpSender CreateSender()
|
private SmtpClient CreateSmtpClient(out string destination)
|
||||||
{
|
{
|
||||||
IConfigurationSection mailSection = _configuration.GetSection("Mail");
|
IConfigurationSection mailSection = _configuration.GetSection("Mail");
|
||||||
string? mailServer = mailSection.GetSection("Server").Value;
|
string? mailServer = mailSection.GetSection("Server").Value;
|
||||||
string? mailUsername = mailSection.GetSection("Username").Value;
|
string? mailUsername = mailSection.GetSection("Username").Value;
|
||||||
string? mailPassword = mailSection.GetSection("Password").Value;
|
string? mailPassword = mailSection.GetSection("Password").Value;
|
||||||
ushort port = mailSection.GetSection("Port").Get<ushort>();
|
ushort port = mailSection.GetSection("Port").Get<ushort>();
|
||||||
|
destination = mailSection.GetSection("Destination").Value ?? string.Empty;
|
||||||
|
|
||||||
var sender = SmtpSender.Create(mailServer, port);
|
var client = new SmtpClient();
|
||||||
sender.SetCredential(mailUsername, mailPassword);
|
client.Connect(mailServer, port, SecureSocketOptions.SslOnConnect);
|
||||||
return sender;
|
client.Authenticate(mailUsername, mailPassword);
|
||||||
|
return client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user