Пример #1
0
        private async Task SendEmailWithSendgridAsync(SendEmail emailSettings, MailAddress toEmail, string subject, string body)
        {
            var mail = new SendGridMessage();

            mail.From = new EmailAddress(emailSettings.FromEmail);
            mail.AddTo(toEmail.Address, toEmail.DisplayName);
            mail.Subject = subject;
            mail.AddContent(MediaTypeNames.Text.Html, body);

            var client   = new SendGridClient(emailSettings.SendgridApiKey);
            var response = await client.SendEmailAsync(mail);

            if (response.StatusCode == HttpStatusCode.Accepted)
            {
                logger.Event($"Email send to '{toEmail.Address}'.");
                logger.ScopeTrace(() => $"Email with subject '{subject}' send to '{toEmail.Address}'.");
            }
            else
            {
                throw new Exception($"Sending email to '{toEmail.Address}' failed with status code '{response.StatusCode}'. Headers '{response.Headers}', body '{await response.Body.ReadAsStringAsync()}'.");
            }
        }