public async Task <SendEmailResponse> SendEmailAsync(SendEmailDetails details)
        {
            var apiKey  = DI.Configuration["SendGrid:Key"];
            var client  = new SendGridClient(apiKey);
            var from    = new EmailAddress(details.FromEmail, details.FromName);
            var to      = new EmailAddress(details.ToEmail, details.ToName);
            var subject = details.Subject;

            var content = details.Content;

            var msg = MailHelper.CreateSingleEmail(
                from,
                to,
                subject,
                details.IsHtml ? null : details.Content,
                details.IsHtml ? details.Content : null);

            var response = await client.SendEmailAsync(msg);

            if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
            {
                return(new SendEmailResponse());
            }

            try
            {
                var bodyResult = await response.Body.ReadAsStringAsync();

                var sendGridResponse = JsonConvert.DeserializeObject <SendGridResponse>(bodyResult);

                var errorResponse = new SendEmailResponse
                {
                    Errors = sendGridResponse?.Errors.Select(f => f.Message).ToList()
                };

                if (errorResponse.Errors == null || errorResponse.Errors.Count() == 0)
                {
                    errorResponse.Errors = new List <string>()
                    {
                        "Неизвестная ошибка от службы отправки электронной почты. Пожалуйста, свяжитесь со службой поддержки"
                    };
                }

                return(errorResponse);
            }
            catch (Exception ex)
            {
                return(new SendEmailResponse
                {
                    Errors = new List <string>()
                    {
                        "Произошла неизвестная ошибка"
                    }
                });
            }
        }
Example #2
0
        public async Task <SendEmailResponse> SendEmailVerificationAsync(SendEmailDetails details, string title, string content1, string content2, string buttonText, string buttonUrl)
        {
            var templateText = default(string);

            using (var reader = new
                                StreamReader(Path.Combine(DI.Environment.WebRootPath, "EmailTemplates/VefifyEmailTemplate.html"),
                                             Encoding.UTF8))
            {
                templateText = await reader.ReadToEndAsync();
            }

            templateText = templateText
                           .Replace("--Title--", title)
                           .Replace("--Content1--", content1)
                           .Replace("--Content2--", content2)
                           .Replace("--ButtonText--", buttonText)
                           .Replace("--ButtonUrl--", buttonUrl);

            details.Content = templateText;

            var emailSender = DI.EmailSender;

            return(await DI.EmailSender.SendEmailAsync(details));
        }