using MailKit.Net.Smtp; using MimeKit; namespace qtc_api.Services.EmailService { public class EmailService : IEmailService { private IConfiguration _configuration; private ILogger _logger; public EmailService(IConfiguration configuration, ILogger logger) { _configuration = configuration; _logger = logger; } public async Task SendConfirmationEmail(string email, string name, string confirmUrl) { // get config entries bool confirmationRequired = _configuration.GetValue("EmailConfig:EmailConfirmationRequired"); string? host = _configuration.GetValue("EmailConfig:SMTPServer"); string? username = _configuration.GetValue("EmailConfig:SMTPUsername"); string? password = _configuration.GetValue("EmailConfig:SMTPPassword"); string? senderAddress = _configuration.GetValue("EmailConfig:SMTPSenderAddress"); if (!confirmationRequired) { _logger.LogInformation("Email Confirmation Is Disabled. For Better Security Of Your Instance, Consider Setting Up An Email Sender Service."); return; } else if (host == null || username == null || password == null || senderAddress == null) { _logger.LogInformation("Email Confirmation Is Enabled But No SMTP Settings Were Set."); return; } // set email subject string emailSubject = "QtC.NET Email Confirmation"; // build confirmation email body StringBuilder emailBody = new(); emailBody.AppendLine($"

Hello {name},

"); emailBody.AppendLine("

Your receiving this message because you made a QtC.NET Account on a server that requires email confirmation.
"); emailBody.AppendLine(@$"You can confirm your account by clicking here.
"); emailBody.AppendLine("NOTE: This Link Is Only Valid For 24 Hours.

"); emailBody.AppendLine("If you did not create a QtC.NET account on any server, you may simply ignore this email.

"); // create new client using var client = new SmtpClient() { RequireTLS = true }; // connect and authenticate await client.ConnectAsync(host, 587); await client.AuthenticateAsync(username, password); // construct email using var message = new MimeMessage(); message.To.Add(new MailboxAddress(name, email)); message.From.Add(new MailboxAddress("QtC.NET Server", senderAddress)); message.Subject = emailSubject; message.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = emailBody.ToString() }; // send email await client.SendAsync(message); // disconnect await client.DisconnectAsync(true); } public async Task SendPasswordResetEmail(string email, string name, string passwordResetUrl) { // get config entries bool confirmationRequired = _configuration.GetValue("EmailConfig:EmailConfirmationRequired"); string? host = _configuration.GetValue("EmailConfig:SMTPServer"); string? username = _configuration.GetValue("EmailConfig:SMTPUsername"); string? password = _configuration.GetValue("EmailConfig:SMTPPassword"); string? senderAddress = _configuration.GetValue("EmailConfig:SMTPSenderAddress"); if (!confirmationRequired) { _logger.LogInformation("Email Confirmation Is Disabled. For Better Security Of Your Instance, Consider Setting Up An Email Sender Service."); return; } else if (host == null || username == null || password == null || senderAddress == null) { _logger.LogInformation("Email Confirmation Is Enabled But No SMTP Settings Were Set."); return; } // set email subject string emailSubject = "QtC.NET Email Confirmation"; // build confirmation email body StringBuilder emailBody = new(); emailBody.AppendLine($"

Hello {name},

"); emailBody.AppendLine("

Your receiving this message because you requested a password reset on a QtC.NET Account.
"); emailBody.AppendLine(@$"You can reset your password by clicking here.
"); emailBody.AppendLine("NOTE: This Link Is Only Valid For 24 Hours.

"); emailBody.AppendLine("If you did not request a password reset on any QtC.NET account on any server, you may simply ignore this email.

"); // create new client using var client = new SmtpClient() { RequireTLS = true }; // connect and authenticate await client.ConnectAsync(host, 587); await client.AuthenticateAsync(username, password); // construct email using var message = new MimeMessage(); message.To.Add(new MailboxAddress(name, email)); message.From.Add(new MailboxAddress("QtC.NET Server", senderAddress)); message.Subject = emailSubject; message.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = emailBody.ToString() }; // send email await client.SendAsync(message); // disconnect await client.DisconnectAsync(true); } } }