using DbusSmsForward.ProcessSmsContent; using DbusSmsForward.Services; using DbusSmsForward.SettingModel; using DbusSmsForward.SMSModel; using MailKit.Net.Smtp; using MimeKit; namespace DbusSmsForward.Notifications; public sealed class EmailNotificationSender(DbusSmsForwardOptions options) : NotificationSenderBase("email") { private readonly EmailConfig _config = options.Notifications.Email; public override bool IsEnabled => _config.Enabled && !string.IsNullOrWhiteSpace(_config.SmtpHost) && !string.IsNullOrWhiteSpace(_config.SendEmail) && !string.IsNullOrWhiteSpace(_config.ReceiveEmail); public override async Task SendAsync(SmsContentModel sms, string body, string deviceName, CancellationToken cancellationToken) { SmsCodeModel code = NotificationContentBuilder.ExtractCode(options, sms); MimeMessage message = new(); message.From.Add(new MailboxAddress("DbusSmsForward", _config.SendEmail)); message.To.Add(new MailboxAddress(string.Empty, _config.ReceiveEmail)); message.Subject = BuildTitle(sms.TelNumber, code.CodeValue); message.Body = new TextPart("plain") { Text = body }; using SmtpClient client = new(); await client.ConnectAsync(_config.SmtpHost, _config.SmtpPort, _config.EnableSsl, cancellationToken); await client.AuthenticateAsync(_config.SendEmail, _config.EmailKey, cancellationToken); await client.SendAsync(message, cancellationToken); await client.DisconnectAsync(true, cancellationToken); } }