Fixed Monitor Mailer Issue and added debug logs

This commit is contained in:
Yinyin Liu 2026-02-02 10:20:33 +01:00
parent fb073a6dca
commit bcaac92e34
3 changed files with 31 additions and 11 deletions

View File

@ -555,6 +555,7 @@ public class Controller : ControllerBase
if (!mail_success) if (!mail_success)
{ {
Db.GetSession(authToken).Delete(newUser); Db.GetSession(authToken).Delete(newUser);
return StatusCode(500, "Welcome email failed to send");
} }
return mail_success ? newUser.HidePassword():Unauthorized(); return mail_success ? newUser.HidePassword():Unauthorized();

View File

@ -296,8 +296,10 @@ public static partial class Db
await user.SendNewUserWelcomeMessage(); await user.SendNewUserWelcomeMessage();
return true; return true;
} }
catch catch (Exception ex)
{ {
Console.WriteLine($"Welcome email failed for {user.Email}");
Console.WriteLine(ex.ToString());
return false; return false;
} }
} }

View File

@ -1,35 +1,52 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text.Json; using System.Text.Json;
using MailKit.Net.Smtp; using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit; using MimeKit;
namespace InnovEnergy.Lib.Mailer; namespace InnovEnergy.Lib.Mailer;
public static class Mailer public static class Mailer
{ {
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")] [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
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(); 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 from = new MailboxAddress(config!.SenderName, config.SenderAddress);
var to = new MailboxAddress(recipientName, recipientEmailAddress); var to = new MailboxAddress(recipientName, recipientEmailAddress);
var msg = new MimeMessage var msg = new MimeMessage
{ {
From = { from }, From = { from },
To = { to }, To = { to },
Subject = subject, Subject = subject,
Body = new TextPart { Text = body } Body = new TextPart("plain") { Text = body }
}; };
using var smtp = new SmtpClient(); using var smtp = new SmtpClient();
await smtp.ConnectAsync(config.SmtpServerUrl, config.SmtpPort, false); try
await smtp.AuthenticateAsync(config.SmtpUsername, config.SmtpPassword); {
await smtp.SendAsync(msg); await smtp.ConnectAsync(config.SmtpServerUrl, config.SmtpPort, SecureSocketOptions.StartTls);
await smtp.DisconnectAsync(true); 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<TValue>(Stream, JsonSerializerOptions, CancellationToken)")] [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.DeserializeAsync<TValue>(Stream, JsonSerializerOptions, CancellationToken)")]