2024-10-23 17:40:50 +01:00
|
|
|
using System.Net.Mail;
|
2024-10-23 22:29:05 +01:00
|
|
|
using System.Security.Authentication;
|
2024-10-23 17:40:50 +01:00
|
|
|
using MailKit.Security;
|
2023-09-22 14:57:04 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Primitives;
|
2024-10-23 17:40:50 +01:00
|
|
|
using MimeKit;
|
|
|
|
using SmtpClient = MailKit.Net.Smtp.SmtpClient;
|
2023-09-22 14:57:04 +01:00
|
|
|
|
|
|
|
namespace OliverBooth.Controllers;
|
|
|
|
|
|
|
|
[Controller]
|
|
|
|
[Route("contact/submit")]
|
|
|
|
public class ContactController : Controller
|
|
|
|
{
|
|
|
|
private readonly ILogger<ContactController> _logger;
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private readonly IConfigurationSection _destination;
|
2024-10-20 04:02:54 +01:00
|
|
|
private readonly IConfigurationSection _sender;
|
2023-09-22 14:57:04 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ContactController" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="configuration">The configuration.</param>
|
|
|
|
public ContactController(ILogger<ContactController> logger, IConfiguration configuration)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_configuration = configuration;
|
2024-10-20 04:07:36 +01:00
|
|
|
|
2024-10-20 04:02:54 +01:00
|
|
|
IConfigurationSection mailConfiguration = configuration.GetSection("Mail");
|
|
|
|
_destination = mailConfiguration.GetSection("Destination");
|
|
|
|
_sender = mailConfiguration.GetSection("Sender");
|
2023-09-22 14:57:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{_?}")]
|
|
|
|
public IActionResult OnGet(string _)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Method GET for endpoint {Path} is not supported!", Request.Path);
|
|
|
|
return RedirectToPage("/Contact/Index");
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("other")]
|
2023-09-25 19:55:34 +01:00
|
|
|
public async Task<IActionResult> HandleForm()
|
2023-09-22 14:57:04 +01:00
|
|
|
{
|
|
|
|
if (!Request.HasFormContentType)
|
|
|
|
{
|
2023-09-25 20:08:05 +01:00
|
|
|
return RedirectToPage("/Contact/Index");
|
2023-09-22 14:57:04 +01:00
|
|
|
}
|
|
|
|
|
2023-09-29 15:47:29 +01:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
_logger.LogError("Captcha validation failed");
|
|
|
|
TempData["Success"] = false;
|
|
|
|
return RedirectToPage("/Contact/Result");
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:57:04 +01:00
|
|
|
IFormCollection form = Request.Form;
|
|
|
|
StringValues name = form["name"];
|
|
|
|
StringValues email = form["email"];
|
|
|
|
StringValues subject = form["subject"];
|
|
|
|
StringValues message = form["message"];
|
|
|
|
|
2024-10-23 17:40:50 +01:00
|
|
|
using SmtpClient client = CreateSmtpClient(out string destination);
|
2023-09-25 19:58:11 +01:00
|
|
|
try
|
|
|
|
{
|
2024-10-23 17:40:50 +01:00
|
|
|
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));
|
2023-09-25 19:58:11 +01:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.LogError(e, "Failed to send email");
|
|
|
|
TempData["Success"] = false;
|
|
|
|
return RedirectToPage("/Contact/Result");
|
|
|
|
}
|
2023-09-22 14:57:04 +01:00
|
|
|
|
|
|
|
TempData["Success"] = true;
|
|
|
|
return RedirectToPage("/Contact/Result");
|
|
|
|
}
|
|
|
|
|
2024-10-23 17:40:50 +01:00
|
|
|
private SmtpClient CreateSmtpClient(out string destination)
|
2023-09-22 14:57:04 +01:00
|
|
|
{
|
|
|
|
IConfigurationSection mailSection = _configuration.GetSection("Mail");
|
|
|
|
string? mailServer = mailSection.GetSection("Server").Value;
|
|
|
|
string? mailUsername = mailSection.GetSection("Username").Value;
|
|
|
|
string? mailPassword = mailSection.GetSection("Password").Value;
|
2024-10-20 04:02:54 +01:00
|
|
|
ushort port = mailSection.GetSection("Port").Get<ushort>();
|
2024-10-23 17:40:50 +01:00
|
|
|
destination = mailSection.GetSection("Destination").Value ?? string.Empty;
|
2023-09-22 14:57:04 +01:00
|
|
|
|
2024-10-23 17:40:50 +01:00
|
|
|
var client = new SmtpClient();
|
2024-10-23 22:29:05 +01:00
|
|
|
client.SslProtocols = SslProtocols.Ssl3 | SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;
|
2024-10-23 22:19:01 +01:00
|
|
|
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
2024-10-23 17:40:50 +01:00
|
|
|
client.Connect(mailServer, port, SecureSocketOptions.SslOnConnect);
|
|
|
|
client.Authenticate(mailUsername, mailPassword);
|
|
|
|
return client;
|
2023-09-22 14:57:04 +01:00
|
|
|
}
|
|
|
|
}
|