public async Task <IActionResult> Test(
            Guid id,
            TestTransportViewModel model,
            [FromServices] IEmailTransportFactory factory)
        {
            if (ModelState.IsValid)
            {
                var transport = await _ctx.FindTransportAsync(id);

                if (transport != null)
                {
                    var impl = factory.CreateTransport(transport);
                    await impl.SendAsync(new Core.SenderParams
                    {
                        Subject = $"Test email using transport {id}",
                        Body    = model.Message,
                        To      = new List <string> {
                            model.EmailAddress
                        }
                    });

                    return(RedirectToAction(nameof(Details), new { id }));
                }

                return(NotFound());
            }

            return(View(model));
        }
示例#2
0
        public async Task <SentEmailInfo> TrySendEmailAsync(EmailMessageParams args)
        {
            EmailTemplate email;

            var templateInfo = await GetTemplateAsync(args);

            if (templateInfo.ApplicationName == null)
            {
                throw new InvalidOperationException($"The application ID `{args.ApplicationId}` does not match any records in the database");
            }

            if (templateInfo.Template == null)
            {
                if (args.TemplateId.HasValue)
                {
                    throw new InvalidOperationException($"Could not find a template matching {args.TemplateId}");
                }
                else
                {
                    throw new InvalidOperationException("No subject and body were supplied, and no template ID was provided");
                }
            }

            // note that Data is a dictionary; if it has no values, we can assume that it's empty
            // and thus skip the transformation
            if (args.Data?.Count > 0)
            {
                email = await Transformer.TransformTemplateAsync(templateInfo.Template, args.Data, args.GetCulture());
            }
            else
            {
                email = templateInfo.Template;
            }

            var senderParams = new SenderParams
            {
                To            = args.To,
                CC            = args.CC,
                Bcc           = args.Bcc,
                Subject       = email.Subject,
                Body          = email.Body,
                SenderName    = templateInfo.SenderName,
                SenderAddress = templateInfo.SenderAddress
            };

            // we can pre-fill these log fields
            var log = new SentEmailInfo
            {
                ApplicationName = templateInfo.ApplicationName,
                TemplateName    = templateInfo.Template.Name,
                TemplateId      = args.TemplateId,
                Subject         = senderParams.Subject,
                LogLevel        = args.LogLevel,
                Recipients      = senderParams.GetRecipients()
            };

            var success = false;

            while (!success && templateInfo.TransportQueue.Any())
            {
                var transportInfo = templateInfo.TransportQueue.Dequeue();
                var transport     = _transportFactory.CreateTransport(transportInfo);

                if (await transport.SendAsync(senderParams))
                {
                    // we can fill this information in now that we know it
                    log.ProcessedUtc = DateTime.UtcNow;
                    log.Transport    = transportInfo;
                    success          = true;
                }
            }

            if (!success)
            {
                throw new Exception("Could not send email with any of the configured transports");
            }

            return(log);
        }