public StartupTasks(BotDbContext db, IOptions <MailingOptions> options, IVkApi api) { _db = db; _api = api; _options = options.Value; InitJobsFromDatabase(); }
/// <summary> /// Создание клиента. /// </summary> /// <param name="options"> Настройки отправки сообщений. </param> /// <returns> Клиент для передачи сообщений. </returns> public static IMailTransport Create(MailingOptions options) { var client = new SmtpClient(); AddTimeout(client, options.TimeOut); return(client); }
public ScheduleTask(BotDbContext db, IScheduleService scheduleService, IEnumerable <ISender> senders, IOptions <MailingOptions> mailingOptions) { _db = db; _scheduleService = scheduleService; _senders = senders; _mailingOptions = mailingOptions.Value; _logger = Log.ForContext <ScheduleTask>(); }
public SendToChatTasks(IScheduleService scheduleService, IWeatherService weatherService, IEnumerable <ISender> senders, IOptions <MailingOptions> mailingOptions, BotDbContext context) { _scheduleService = scheduleService; _weatherService = weatherService; _senders = senders; _context = context; _mailingOptions = mailingOptions.Value; _logger = Log.ForContext <SendToChatTasks>(); }
/// <summary> /// Конфвертация сообщения в пригодный для отправки вид. /// </summary> /// <param name="message"> Сообщение. </param> /// <param name="mailingOptions"> Параметры. </param> /// <returns> Сообщение для отправки. </returns> public static MimeMessage Convert(EmailMessage message, MailingOptions mailingOptions) { var body = ConvertBody(message.Body); var recepients = message.Recepients .Select(ConvertRecepient) .ToList(); var sender = CreateSender(mailingOptions); var result = new MimeMessage { Subject = message.Subject, Body = body }; result.To.AddRange(recepients); result.From.Add(sender); return(result); }
public async Task <bool> SendMailAsync(string address, string name, string subject, string content) { if (disposedValue) { throw new ObjectDisposedException(nameof(MailingService)); } MailingOptions options = this.options.Value; if (!options.EnableMailing) { logger.LogWarning($"Mailing is disabled. {name} <{address}> will not receive an email."); return(true); } var sender = new MailboxAddress(options.SenderName, options.SenderAddress); var author = string.IsNullOrEmpty(options.AuthorAddress) ? sender : new MailboxAddress(options.AuthorName, options.AuthorAddress); var message = new MimeMessage(); message.From.Add(author); message.Sender = sender; message.To.Add(new MailboxAddress(name, address)); message.Subject = subject; message.Body = new TextPart("plain") { Text = content }; try { if (client == null) { client = new SmtpClient(); await client.ConnectAsync(options.SmtpHost, options.SmtpPort, options.UseSsl); await client.AuthenticateAsync(options.SmtpUsername, options.SmtpPassword); } // We have to handle the case of an SMTP rate limit when multiple customers are supposed to receive a mail at the same time // Strato for reference only allows you to send 50 emails without delay (September 2021) // // RFC 821 defines common status code as 450 mailbox unavailable (busy or blocked for policy reasons) // RFC 3463 defines enhanced status code as 4.7.X for persistent transient failures caused by security or policy status await client.SendAsync(message); logger.LogInformation($"Successfully sent email to {address}."); return(true); } catch (SmtpCommandException ex) when(ex.StatusCode == SmtpStatusCode.ServiceNotAvailable) { logger.LogInformation("SMTP session closed by server. This is most likely just a normal idle timeout."); return(false); } catch (SmtpCommandException ex) when(ex.StatusCode == SmtpStatusCode.MailboxBusy) { logger.LogInformation("Mailbox busy. This is most likely caused by a temporary rate limit."); return(false); } catch (Exception ex) { logger.LogError(ex, $"Failed to send mail to {name} <{address}>."); if (client != null) { await client.DisconnectAsync(quit : true); client.Dispose(); client = null; } return(true); } }
public MailingService( IOptions <MailingOptions> options) { this.options = options.Value; }
private static MailboxAddress CreateSender(MailingOptions mailingOptions) { var displayName = mailingOptions.DisplayName ?? mailingOptions.Email; return(new MailboxAddress(displayName, mailingOptions.Email)); }
public HomeController(IOptionsSnapshot <MailingOptions> mailingOptions, IOptions <FeedOptions> feedOptions) { _mailingOptions = mailingOptions.Value; _feedOptions = feedOptions.Value; }