Exemplo n.º 1
0
        public async Task SendMailAsync(IEnumerable <string> toAddress,
                                        TemplateEngine templateEngine = null, string ccAddress = null)
        {
            var enumerable = toAddress as string[] ?? toAddress.ToArray();

            if (!enumerable.Any())
            {
                throw new ArgumentNullException(nameof(toAddress));
            }

            switch (CurrentEngine)
            {
            case null when templateEngine == null:
                throw new Exception("TemplateEngine must be specified.");

            case null when true:
                CurrentEngine = templateEngine;
                break;
            }

            // create mail message
            var messageToSend = new MimeMessage
            {
                Sender  = new MailboxAddress(Settings.SenderName, Settings.SmtpUserName),
                Subject = CurrentEngine.Format(() => new StringBuilder(CurrentEngine.TextProvider.Subject)).Trim(),
            };

            messageToSend.From.Add(new MailboxAddress(Settings.EmailDisplayName, Settings.SmtpUserName));
            var bodyText = CurrentEngine.Format(() => new StringBuilder(CurrentEngine.TextProvider.Text)).Trim();

            if (CurrentEngine.TextProvider is TemplateMailMessage templateMailMessage && templateMailMessage.IsHtml)
            {
                messageToSend.Body = new TextPart(TextFormat.Html)
                {
                    Text = bodyText
                };
            }
Exemplo n.º 2
0
        public EmailHelper ApplyTemplate(string mailType, TemplatePipeline pipeline, Action emailSentAction = null, Action emailFailedAction = null)
        {
            var mailConfig = XmlSection<MailConfiguration>.GetSection("mailConfiguration");
            if (mailConfig.CommonConfiguration.OverrideToAddress)
            {
                ToAddressOverride = mailConfig.CommonConfiguration.ToAddress;
            }

            var messageToPersonalize = new TemplateMailMessage(mailType);
            if (messageToPersonalize.Loaded)
            {
                var engine = new TemplateEngine(messageToPersonalize, pipeline);
                CurrentEngine = engine;
                return this;
            }
            return null;
        }