public async Task SendSalesReportEmailAsync(DateTime date) { var orders = orderService.Get(date.AddDays(-7), date); if (!orders.Any()) { return; } salesReportBuilder.AddOrders(orders); salesReportBuilder.AddOrders(orders); SalesReport report = salesReportBuilder.Build(); // dotnet add package SendGrid SendGridClient client = new SendGridClient(apikey); var recipients = userService.GetBosses(); recipients = recipients.Where(r => !string.IsNullOrEmpty(r.Email)); var sender = userService.GetBot(); foreach (var recipient in recipients) { var message = MailHelper.CreateSingleEmail( new EmailAddress(sender.Email, $"{sender.FirstName} {sender.LastName}"), new EmailAddress(recipient.Email, $"{recipient.FirstName} {recipient.LastName}"), "Raport sprzedaży", report.ToString(), report.ToHtml()); Logger.Info($"Wysyłanie raportu do {recipient.FirstName} {recipient.LastName} <{recipient.Email}>..."); var response = await client.SendEmailAsync(message); if (response.StatusCode == System.Net.HttpStatusCode.Accepted) { ReportSent?.Invoke(this, new ReportSentEventArgs(DateTime.Now)); Logger.Info($"Raport został wysłany."); } else { Logger.Error($"Błąd podczas wysyłania raportu."); throw new ApplicationException("Błąd podczas wysyłania raportu."); } } }
public async Task Send( SalesReport report, User sender, IEnumerable <User> recipients) { foreach (var recipient in recipients) { if (recipient.Email == null) { continue; } var message = MailHelper.CreateSingleEmail( new EmailAddress(sender.Email, $"{sender.FirstName} {sender.LastName}"), new EmailAddress(recipient.Email, $"{recipient.FirstName} {recipient.LastName}"), "Raport sprzedaży", report.ToString(), report.ToHtml()); logger.Info($"Wysyłanie raportu do {recipient.FirstName} {recipient.LastName} <{recipient.Email}>..."); var response = await client.SendEmailAsync(message); if (response.StatusCode == System.Net.HttpStatusCode.Accepted) { ReportSent?.Invoke(this, new ReportSentEventArgs(DateTime.Now)); logger.Info($"Raport został wysłany."); } else { logger.Error($"Błąd podczas wysyłania raportu."); throw new ApplicationException("Błąd podczas wysyłania raportu."); } } }