40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|