using System.Net.Security; using MailKit.Net.Smtp; using MailKit.Security; using MimeKit; namespace landing.Services; public class EmailService { private readonly IConfiguration _config; private readonly ILogger _logger; public EmailService(IConfiguration config, ILogger logger) { _config = config; _logger = logger; } public async Task SendConfirmationEmailAsync(string email, string unsubscribeLink) { var subject = "Thanks for subscribing!"; var htmlBody = $@"

Welcome to RideAware!

Thank you for subscribing to our newsletter.

Unsubscribe

"; await SendEmailAsync(email, subject, htmlBody); } public async Task SendContactConfirmationAsync(string email, string name) { var subject = "We received your message - RideAware"; var escapedName = System.Net.WebUtility.HtmlEncode(name); var htmlBody = $@"

Thank you for reaching out, {escapedName}!

We've received your message and will get back to you as soon as possible.

In the meantime, feel free to check out more about RideAware on our website.

Best regards,
The RideAware Team

"; await SendEmailAsync(email, subject, htmlBody); } public async Task SendContactNotificationAsync(string adminEmail, string name, string email, string contactSubject, string message) { var emailSubject = $"New contact message from {name}"; var escapedName = System.Net.WebUtility.HtmlEncode(name); var escapedEmail = System.Net.WebUtility.HtmlEncode(email); var escapedSubject = System.Net.WebUtility.HtmlEncode(contactSubject); var escapedMessage = System.Net.WebUtility.HtmlEncode(message).Replace("\n", "
"); var htmlBody = $@"

New Contact Message

From: {escapedName} ({escapedEmail})

Subject: {escapedSubject}

Message:

{escapedMessage}

"; await SendEmailAsync(adminEmail, emailSubject, htmlBody); } private async Task SendEmailAsync(string toEmail, string subject, string htmlBody) { var smtpHost = _config["SMTP_SERVER"] ?? throw new InvalidOperationException("SMTP_SERVER not configured"); var smtpPort = int.Parse(_config["SMTP_PORT"] ?? "465"); var smtpUser = _config["SMTP_USER"] ?? throw new InvalidOperationException("SMTP_USER not configured"); var smtpPass = _config["SMTP_PASSWORD"] ?? throw new InvalidOperationException("SMTP_PASSWORD not configured"); var message = new MimeMessage(); message.From.Add(MailboxAddress.Parse(smtpUser)); message.To.Add(MailboxAddress.Parse(toEmail)); message.Subject = subject; message.Body = new TextPart("html") { Text = htmlBody }; using var client = new SmtpClient(); // Accept the server's certificate (matching Go's TLS behavior) client.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => errors == SslPolicyErrors.None || errors == SslPolicyErrors.RemoteCertificateChainErrors; // Port 465 uses direct SSL/TLS (SslOnConnect) await client.ConnectAsync(smtpHost, smtpPort, SecureSocketOptions.SslOnConnect); await client.AuthenticateAsync(smtpUser, smtpPass); await client.SendAsync(message); await client.DisconnectAsync(true); } }