diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index 8270d4c40..1fd23700e 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -555,6 +555,7 @@ public class Controller : ControllerBase if (!mail_success) { Db.GetSession(authToken).Delete(newUser); + return StatusCode(500, "Welcome email failed to send"); } return mail_success ? newUser.HidePassword():Unauthorized(); diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index f83e33cac..7d21eee60 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -296,8 +296,10 @@ public static partial class Db await user.SendNewUserWelcomeMessage(); return true; } - catch + catch (Exception ex) { + Console.WriteLine($"Welcome email failed for {user.Email}"); + Console.WriteLine(ex.ToString()); return false; } } diff --git a/csharp/Lib/Mailer/Mailer.cs b/csharp/Lib/Mailer/Mailer.cs index 74781ce14..87563cef0 100644 --- a/csharp/Lib/Mailer/Mailer.cs +++ b/csharp/Lib/Mailer/Mailer.cs @@ -1,35 +1,52 @@ using System.Diagnostics.CodeAnalysis; using System.Text.Json; using MailKit.Net.Smtp; +using MailKit.Security; using MimeKit; namespace InnovEnergy.Lib.Mailer; - public static class Mailer { [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] - public static async Task Send(String recipientName, String recipientEmailAddress, String subject, String body) + public static async Task Send(string recipientName, string recipientEmailAddress, string subject, string body) { var config = await ReadMailerConfig(); + + Console.WriteLine("=============== SMTP CONFIG LOADED =============="); + Console.WriteLine($"Config full path: {Path.GetFullPath(MailerConfig.DefaultFile)}"); + Console.WriteLine($"SMTP host: {config!.SmtpServerUrl}"); + Console.WriteLine($"SMTP port: {config.SmtpPort}"); + Console.WriteLine($"SMTP username: {config.SmtpUsername}"); + Console.WriteLine($"Sender: {config.SenderName} <{config.SenderAddress}>"); + Console.WriteLine("=================================================="); + var from = new MailboxAddress(config!.SenderName, config.SenderAddress); var to = new MailboxAddress(recipientName, recipientEmailAddress); - - var msg = new MimeMessage + + var msg = new MimeMessage { From = { from }, To = { to }, Subject = subject, - Body = new TextPart { Text = body } + Body = new TextPart("plain") { Text = body } }; using var smtp = new SmtpClient(); - - await smtp.ConnectAsync(config.SmtpServerUrl, config.SmtpPort, false); - await smtp.AuthenticateAsync(config.SmtpUsername, config.SmtpPassword); - await smtp.SendAsync(msg); - await smtp.DisconnectAsync(true); + + try + { + await smtp.ConnectAsync(config.SmtpServerUrl, config.SmtpPort, SecureSocketOptions.StartTls); + await smtp.AuthenticateAsync(config.SmtpUsername, config.SmtpPassword); + await smtp.SendAsync(msg); + await smtp.DisconnectAsync(true); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + throw; // keep while testing + } } [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.DeserializeAsync(Stream, JsonSerializerOptions, CancellationToken)")]