This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using System.Net.Mail;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace plushealthtech.Mail
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends outbound email through the configured SMTP server (MailPit in local
|
||||
/// development). No authentication is used.
|
||||
/// </summary>
|
||||
public class MailSender
|
||||
{
|
||||
private readonly SmtpOptions _options;
|
||||
private readonly ILogger<MailSender> _logger;
|
||||
|
||||
public MailSender(IOptions<SmtpOptions> options, ILogger<MailSender> logger)
|
||||
{
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task SendAsync(string subject, string body)
|
||||
{
|
||||
using var client = new SmtpClient(_options.Host, _options.Port)
|
||||
{
|
||||
EnableSsl = false
|
||||
};
|
||||
|
||||
var message = new MailMessage(_options.From, _options.To)
|
||||
{
|
||||
Subject = subject,
|
||||
Body = body,
|
||||
IsBodyHtml = false
|
||||
};
|
||||
|
||||
await client.SendMailAsync(message);
|
||||
_logger.LogInformation("Mail sent to {To} (subject: {Subject})", _options.To, subject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace plushealthtech.Mail
|
||||
{
|
||||
/// <summary>
|
||||
/// SMTP settings bound from the "Smtp" section of appsettings.json.
|
||||
/// </summary>
|
||||
public class SmtpOptions
|
||||
{
|
||||
public string Host { get; set; } = "localhost";
|
||||
public int Port { get; set; } = 1025;
|
||||
public string From { get; set; } = string.Empty;
|
||||
public string To { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user