private SendGridEmailMessage CreateMessage(string subject = null, params string[] receivers) { var customReceivers = receivers ?? Enumerable.Empty <string>(); var emailReceivers = (_configuration.DefaultReceivers.Any() ? _configuration.DefaultReceivers.Union(customReceivers) : customReceivers).ToList(); if (!emailReceivers.Any()) { throw new ArgumentException("Email message receivers have not been defined.", nameof(emailReceivers)); } var emailSubject = string.IsNullOrWhiteSpace(subject) ? _configuration.DefaultSubject : subject; emailReceivers.ValidateEmails(nameof(receivers)); var emailMessage = new SendGridEmailMessage { Subject = emailSubject, From = new SendGridEmailMessage.Person { Email = _configuration.Sender } }; emailMessage.Personalizations.Add(new SendGridEmailMessage.Personalization { To = emailReceivers.Select(x => new SendGridEmailMessage.Person { Email = x }).ToList() }); return(emailMessage); }
public async Task SendMessageAsync(string apiKey, SendGridEmailMessage message) { _httpClient.DefaultRequestHeaders.Remove("Authorization"); _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); var payload = JsonConvert.SerializeObject(message); var content = new StringContent(payload, Encoding.UTF8, "application/json"); await _httpClient.PostAsync("mail/send", content); }
private async Task SendMessageAsync(SendGridEmailMessage message) { var emailSender = _configuration.EmailSenderProvider(); await emailSender.SendMessageAsync(_configuration.ApiKey, message); }