Ejemplo n.º 1
0
        public async Task <EmailSendResult> SendWithAttachments(
            List <KeyValuePair <string, string> > keyValues,
            ElasticEmailOptions options,
            string subject,
            List <EmailAttachment> attachments)
        {
            var endpoint = apiEndpoint;

            if (!string.IsNullOrWhiteSpace(options.EndpointUrl))
            {
                endpoint = options.EndpointUrl;
            }
            var client = _httpClientFactory.GetOrCreateHttpClient(new Uri(endpoint));

            using (var formData = new MultipartFormDataContent())
            {
                foreach (var item in keyValues)
                {
                    HttpContent stringContent = new StringContent(item.Value);
                    formData.Add(stringContent, item.Key);
                }

                int i = 0;
                foreach (var attachement in attachments)
                {
                    HttpContent fileStreamContent = new StreamContent(attachement.Stream);
                    formData.Add(fileStreamContent, "file" + i, attachement.FileName);
                    i++;
                }

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

                    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));
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> IsConfigured(string configLookupKey = null)
        {
            if (options == null)
            {
                options = await _optionsProvider.GetElasticEmailOptions(configLookupKey);
            }

            if (options == null || string.IsNullOrWhiteSpace(options.ApiKey))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 3
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));
        }