예제 #1
0
        public async Task SendEmailAsync(MailgunTemplateEmail email)
        {
            var url = new UriBuilder(_httpClient.BaseAddress)
            {
                Port = -1,
                Path = $"v3/{Config.Domain}/messages"
            };

            var jsonVariables = JsonSerializer.Serialize(email.Variables);

            var requestContent = new MultipartFormDataContent();

            requestContent.Add(new StringContent(email.From), "from");
            requestContent.Add(new StringContent(email.To), "to");
            requestContent.Add(new StringContent(email.Subject), "subject");
            requestContent.Add(new StringContent(email.Template), "template");
            requestContent.Add(new StringContent(jsonVariables), "h:X-Mailgun-Variables");

            if (email.Attachments.Any())
            {
                foreach (var attachment in email.Attachments)
                {
                    var fileContent = new ByteArrayContent(attachment.Bytes);
                    fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(attachment.ContentType);
                    requestContent.Add(fileContent, "attachment", attachment.FileName);
                }
            }

            var request = new HttpRequestMessage(HttpMethod.Post, url.ToString());

            request.Content = requestContent;
            var response = await _httpClient.SendAsync(request);

            response.EnsureSuccessStatusCode();
        }
예제 #2
0
        public async Task SendEmailAsync(MailgunTemplateEmail email)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            await _notifications.PushFakeApiUsedAsync("mailgun", new
            {
                email.From,
                email.To,
                email.Template,
                email.Variables,
                Attachments = string.Join(", ", email.Attachments.Select(x => x.FileName))
            });
        }