Esempio n. 1
0
        private async Task <EmailSendResult> SendWithoutAttachments(List <KeyValuePair <string, string> > keyValues, MailgunOptions options, string subject)
        {
            var client = _httpClientFactory.GetOrCreateHttpClient(new Uri(options.EndpointUrl));

            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(PrefixApiKey(options.ApiKey))));

            var content = new FormUrlEncodedContent(keyValues);

            try
            {
                var response = await client.PostAsync("messages", content).ConfigureAwait(false);

                var result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    var message = $"failed to send email with subject {subject} error was {response.StatusCode} : {result}";
                    _log.LogError(message);
                    return(new EmailSendResult(false, message));
                }
            }
            catch (Exception ex)
            {
                var message = $"failed to send email with subject {subject} error was {ex.Message} : {ex.StackTrace}";
                _log.LogError(message);
                return(new EmailSendResult(false, message));
            }

            return(new EmailSendResult(true));
        }
Esempio n. 2
0
        private async Task <EmailSendResult> SendWithoutAttachments(List <KeyValuePair <string, string> > keyValues, ElasticEmailOptions options, string subject)
        {
            var content  = new FormUrlEncodedContent(keyValues);
            var endpoint = apiEndpoint;

            if (!string.IsNullOrWhiteSpace(options.EndpointUrl))
            {
                endpoint = options.EndpointUrl;
            }

            var client = _httpClientFactory.GetOrCreateHttpClient(new Uri(endpoint));

            try
            {
                var response = await client.PostAsync("email/send", content).ConfigureAwait(false);

                var result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode || result.Contains("Oops"))
                {
                    var message = $"failed to send email with subject {subject} error was {response.StatusCode} : {result}";
                    _log.LogError(message);
                    return(new EmailSendResult(false, message));
                }
            }
            catch (Exception ex)
            {
                var message = $"failed to send email with subject {subject} error was {ex.Message} : {ex.StackTrace}";
                _log.LogError(message);
                return(new EmailSendResult(false, message));
            }

            return(new EmailSendResult(true));
        }