Exemple #1
0
        private async Task <EmailTemplateInfo> GetTemplateAsync(EmailMessageParams args)
        {
            var response = new EmailTemplateInfo();

            var application = await GetApplicationAsync(args.ApplicationId);

            if (application == null)
            {
                return(response);
            }

            response.ApplicationName = application.Name;
            response.SenderAddress   = application.SenderAddress;
            response.SenderName      = application.SenderName;

            var transports = (await GetTransportsAsync(args.ApplicationId)).ToArray();

            response.TransportQueue = new Queue <ITransportDefinition>(transports);

            if (args.TemplateId.HasValue)
            {
                response.Template = await GetTemplateAsync(args.TemplateId.Value, args.GetCulture());
            }
            else
            {
                response.Template = new EmailTemplate(args.Subject, args.GetBody(), args.GetCulture());
            }

            return(response);
        }
Exemple #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);
        }
Exemple #3
0
 public static string ToJson(EmailMessageParams message, Formatting formatting = Formatting.Indented)
 {
     return(JsonConvert.SerializeObject(message, formatting));
 }