Exemple #1
0
        /// <inheritdoc/>
        public override async Task SendAsync <TModel>(string[] recipients, string subject, string body, string template, TModel data, FileAttachment[] attachments = null)
        {
            var bccRecipients      = (_settings.BccRecipients ?? "").Split(';', ',');
            var recipientAddresses = recipients.Select(recipient => new SparkPostRecipient {
                Address = new SparkPostRecipientEmailAddress {
                    Email = recipient
                }
            });
            var bccAddresses = bccRecipients.SelectMany(bcc => recipients.Select(recipient => new SparkPostRecipient {
                Address = new SparkPostRecipientEmailAddress {
                    Email    = bcc,
                    HeaderTo = recipient
                }
            }));
            var request = new SparkPostRequest {
                Content = new SparkPostContent {
                    From = new SparkPostSenderAddress {
                        Email = _settings.Sender,
                        Name  = _settings.SenderName
                    },
                    Subject = subject,
                    Html    = await GetHtmlAsync(body, subject, template.ToString(), data)
                },
                Recipients = recipientAddresses.Concat(bccAddresses).ToArray()
            };

            if (attachments?.Length > 0)
            {
                var attachmentsList = new List <SparkPostAttachment>();
                foreach (var attachment in attachments)
                {
                    attachmentsList.Add(new SparkPostAttachment {
                        Name = attachment.FileName,
                        Type = FileExtensions.GetMimeType(Path.GetExtension(attachment.FileName)),
                        Data = Convert.ToBase64String(attachment.Data)
                    });
                }
                request.Content.Attachments = attachmentsList.ToArray();
            }
            var requestJson = JsonSerializer.Serialize(request, new JsonSerializerOptions {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
#if NET5_0_OR_GREATER
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
#else
                IgnoreNullValues = true,
#endif
            });
            var response = await _httpClient.PostAsync("transmissions", new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json));

            if (!response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var message = $"SparkPost service could not send email to recipients '{string.Join(", ", recipients)}'. Error is: '{content}'.";
                _logger.LogError(message);
                throw new InvalidOperationException(message);
            }
        }
Exemple #2
0
        /// <inheritdoc/>
        public override async Task SendAsync <TModel>(string[] recipients, string subject, string body, string template, TModel data, FileAttachment[] attachments = null)
        {
            var request = new SparkPostRequest {
                Content = new SparkPostContent {
                    From    = _settings.Sender,
                    Subject = subject,
                    Html    = await GetHtmlAsync(body, subject, template.ToString(), data)
                },
                Recipients = recipients.Select(recipient => new SparkPostRecipient {
                    Address = recipient
                }).ToArray()
            };

            if (attachments?.Length > 0)
            {
                var attachmentsList = new List <SparkPostAttachment>();
                foreach (var attachment in attachments)
                {
                    attachmentsList.Add(new SparkPostAttachment {
                        Name = attachment.FileName,
                        Type = FileExtensions.GetMimeType(Path.GetExtension(attachment.FileName)),
                        Data = Convert.ToBase64String(attachment.Data)
                    });
                }
                request.Content.Attachments = attachmentsList.ToArray();
            }
            var requestJson = JsonConvert.SerializeObject(request, new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
            var response = await _httpClient.PostAsync("transmissions", new StringContent(requestJson, Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var message = $"SparkPost service could not send email to recipients '{string.Join(", ", recipients)}'. Error is: '{content}'.";
                _logger.LogError(message);
                throw new InvalidOperationException(message);
            }
        }