コード例 #1
0
        public async Task <IActionResult> Post([FromBody] EmailSendRequest request)
        {
            bool isValidEmailRequest = EmailSendRequestFactory.IsValidEmailRequest(request);

            if (!isValidEmailRequest)
            {
                return(new BadRequestObjectResult("Invalid email payload."));
            }

            bool hasNoContent = request.Content == null;

            if (hasNoContent)
            {
                request.Content = new JObject();
            }

            bool hasNoPersonalContent = request.PersonalContent == null;

            if (hasNoPersonalContent)
            {
                request.PersonalContent = new JObject();
            }

            MailAddress[] toAddresses;

            try
            {
                toAddresses = request.To.Select(t => new MailAddress(t)).ToArray();
            }
            catch (Exception exception)
            {
                return(new BadRequestObjectResult($"Invalid format of recipient email {request.To}."));
            }

            Template template = TemplateUtility.GetTemplateByName(_emailConfiguration, request.Template);

            bool isInvalidTemplate = template == null;

            if (isInvalidTemplate)
            {
                return(new BadRequestObjectResult($"A template with the name {request.Template} does not exist."));
            }

            JObject fullContent = JsonUtility.GetMergedJson(request.Content, request.PersonalContent);

            EmailViewModel emailViewModel = new EmailViewModel()
            {
                TemplateName = template.Name, Content = fullContent
            };
            string rawHtml = await _htmlGeneratorService.GetRawHtmlAsync("Email/Index", emailViewModel);

            bool hasNoRawHtml = rawHtml == null;

            if (hasNoRawHtml)
            {
                return(new BadRequestObjectResult("Internal error."));
            }

            Email email = new Email(toAddresses, template.Subject, ContentType.TEXT_HTML, rawHtml);
            EmailPreviewResponse response = EmailPreviewResponseFactory.Create(email);

            return(new OkObjectResult(response));
        }
コード例 #2
0
        public async Task <IActionResult> Post([FromBody] EmailSendRequest request)
        {
            bool isValidEmailRequest = EmailSendRequestFactory.IsValidEmailRequest(request);

            if (!isValidEmailRequest)
            {
                return(new BadRequestObjectResult("Invalid email payload."));
            }

            MailAddress[] toAddresses;

            try
            {
                toAddresses = request.To.Select(t => new MailAddress(t)).ToArray();
            }
            catch (Exception exception)
            {
                return(new BadRequestObjectResult($"Invalid format of recipient email {request.To}."));
            }

            bool hasAtLeastOneEmailAddress = toAddresses.Length > 0;

            if (!hasAtLeastOneEmailAddress)
            {
                return(new BadRequestObjectResult("No receivers specified. Need at least one."));
            }

            Template template = TemplateUtility.GetTemplateByName(_emailConfiguration, request.Template);

            bool isInvalidTemplate = template == null;

            if (isInvalidTemplate)
            {
                return(new BadRequestObjectResult($"A template with the name {request.Template} does not exist."));
            }

            JObject fullContent = JsonUtility.GetMergedJson(request.Content, request.PersonalContent);

            EmailViewModel emailViewModel = new EmailViewModel()
            {
                TemplateName = template.Name, Content = fullContent
            };
            string rawHtml = await _htmlGeneratorService.GetRawHtmlAsync("Email/Index", emailViewModel);

            bool hasNoRawHtml = rawHtml == null;

            if (hasNoRawHtml)
            {
                return(new BadRequestObjectResult("Internal error."));
            }

            Email email = new Email(toAddresses, template.Subject, ContentType.TEXT_HTML, rawHtml);

            string emailServiceId;

            try
            {
                emailServiceId = await _emailService.SendEmailAsync(email);
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult("Failed to send email."));
            }

            try
            {
                string[] receiverEmails = email.To.Select(t => t.ToString()).ToArray();

                await _emailLoggingService.LogAsync(emailServiceId, receiverEmails, template.Name,
                                                    request.PersonalContent, request.Content);

                return(new OkResult());
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult("Email was sent successfully, but logging failed unexpectedly."));
            }
        }