Beispiel #1
0
 public static void SendMail(this IEmailModel model)
 {
     foreach (var email in model.CreateEmailMessage())
     {
         SenderManager.Send(email);
     }
 }
Beispiel #2
0
 public static void SendMailAsync(this IEmailModel model)
 {
     foreach (var email in model.CreateEmailMessage())
     {
         email.SendMailAsync();
     }
 }
        public async Task QueueMessageAsync(ClaimsPrincipal user, IEmailModel email, MailAddress recipient, bool replyToUser)
        {
            MailAddress userAddress = null;

            if (user != null)
            {
                using (var scope = _scopeFactory.CreateScope())
                {
                    var userManager = scope.ServiceProvider.GetRequiredService <IUserManager>();
                    userAddress = new MailAddress(userManager.GetEmailAddress(user), userManager.GetUserName(user));
                }
            }

            if (_overrideRecipient.Enabled)
            {
                if (_overrideRecipient.Fixed == null)
                {
                    if (user == null)
                    {
                        return;
                    }

                    recipient = userAddress;
                }
                else
                {
                    recipient = new MailAddress(_overrideRecipient.Fixed.Address, _overrideRecipient.Fixed.DisplayName);
                }
            }

            await SendEmailAsync(email, recipient, replyToUser&& userAddress != null?userAddress : null);
        }
        public async Task SendAsync(IEmailModel model)
        {
            var subject = _localizer[$"{model.Template}_Subject"].ToString().Replace(model);
            var html    = _localizer[$"{model.Template}_Html"].ToString().Replace(model);

            var message = new MailMessage();

            message.To.Add(model.ToAddress);
            message.From       = new MailAddress(_emailSettings.NoReply);
            message.Subject    = subject;
            message.IsBodyHtml = true;
            message.Body       = html;

            try
            {
                using (var client = new SmtpClient(_emailSettings.Host, _emailSettings.Port))
                {
                    client.Credentials = new NetworkCredential(_emailSettings.Username, _emailSettings.Password);
                    await client.SendMailAsync(message);
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Email Send Failed");
            }
        }
        public EmailWindowPresenter(IEmailWindow view, IEmailModel model)
        {
            this.View   = view;
            this._model = model;

            // subscribe to the view's events
            this.View.SaveAndSendButtonClicked += SaveAndSendButtonClicked;
            this.View.InputFieldTextChanged    += InputFieldTextChanged;
        }
Beispiel #6
0
        public ConfigWindowPresenter(IConfigWindow view, IEmailModel model)
        {
            this._model = model;
            this.View   = view;

            // subscribe to the view's events
            this.View.InputFieldChanged   += InputFieldChanged;
            this.View.SaveButtonClicked   += SaveButtonClicked;
            this.View.CancelButtonClicked += CancelButtonClicked;
        }
Beispiel #7
0
        public static IEnumerable <EmailMessageEntity> CreateEmailMessage(this IEmailModel emailModel)
        {
            if (emailModel.UntypedEntity == null)
            {
                throw new InvalidOperationException("Entity property not set on EmailModel");
            }

            using (IsolationEntity.Override((emailModel.UntypedEntity as Entity)?.TryIsolation()))
            {
                var emailModelEntity = ToEmailModelEntity(emailModel.GetType());
                var template         = GetDefaultTemplate(emailModelEntity, emailModel.UntypedEntity as Entity);

                return(EmailTemplateLogic.CreateEmailMessage(template.ToLite(), model: emailModel));
            }
        }
        public static async Task Send(IEmailModel model)
        {
            Func <string, string> getManifestResource = s =>
            {
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(s))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        return(reader.ReadToEnd());
                    }
                };
            };

            var templateId    = string.Format("GeneratedEmail_{0}", model.View);
            var emailTemplate = getManifestResource(model.View);

            Platform.RazorEngine.AddTemplate(templateId, emailTemplate);

            var body = Platform.RazorEngine.Run(templateId, model);

            MailMessage message = new MailMessage();

            message.Subject    = model.Subject;
            message.IsBodyHtml = true;
            message.To.Add(model.ToAddress);
            message.From = model.FromAddress;

            var view = AlternateView.CreateAlternateViewFromString(body, Encoding.UTF8, MediaTypeNames.Text.Html);

            var resources = FindLinkedResources(body);

            foreach (var resource in resources)
            {
                view.LinkedResources.Add(resource);
            }

            message.AlternateViews.Add(view);

            using (var smtpClient = new SmtpClient("smtp.postmarkapp.com", 2525)
            {
                Credentials = new NetworkCredential("cbcdf121-f50b-465e-8886-7c39ee1d9b34", "cbcdf121-f50b-465e-8886-7c39ee1d9b34")
            })
            {
                await smtpClient.SendMailAsync(message);
            }
        }
        private async Task SendEmailAsync(IEmailModel email, MailAddress recipient, MailAddress replyTo)
        {
            var templatePath = Path.ChangeExtension(email.TemplateName, "hbs");

            var body = await _renderService.RenderAsync(templatePath, email);

            var message = new MailMessage(_sender, recipient)
            {
                Subject    = email.Subject,
                Body       = body,
                IsBodyHtml = email.IsHtml
            };

            if (replyTo != null)
            {
                message.ReplyToList.Add(replyTo);
            }

            await _queue.SendAsync(message);
        }
        private static void SendMail(IEmailModel emailModel)
        {
            using (var mm = new MailMessage())
            {
                mm.From = emailModel.FromAddress;
                mm.To.Add(emailModel.ToAddress);
                mm.IsBodyHtml = true;
                mm.Subject    = emailModel.Subject;
                mm.Body       = emailModel.AttachmentReport;

                using (var sc = new SmtpClient("mail2.aerotur.aero", 25))
                {
                    ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
                    //sc.EnableSsl = true;
                    sc.DeliveryMethod        = SmtpDeliveryMethod.Network;
                    sc.UseDefaultCredentials = false;
                    sc.Credentials           = new NetworkCredential("*****@*****.**", "rtbKLier7ft2D6A");
                    sc.Send(mm);
                    ServicePointManager.ServerCertificateValidationCallback = null;
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// Send the email
        ///
        /// Looks for templates
        /// `EmailTemplates/{templateName}(.{languageCode}).Subject.txt` and
        /// `EmailTemplates/{templateName}(.{languageCode}).Body.html`
        ///
        /// In addition to `model` also Option section `EmailPlaceholders` is used
        /// in placeholders.
        /// </summary>
        /// <param name="templateName">Name of the template, </param>
        /// <param name="languageCode"></param>
        /// <param name="toEmail"></param>
        /// <param name="toName"></param>
        /// <param name="model">Used as placeholders</param>
        /// <exception cref="IOException">Thrown when the template can't be read</exception>
        /// <returns></returns>
        private async Task Send(string templateName, string languageCode, string toEmail, string toName, IEmailModel model)
        {
            var props = model.GetType().GetProperties();

            var subject         = new StringBuilder("");
            var body            = new StringBuilder("");
            var values          = new Dictionary <string, string>();
            var languageSuffix  = languageCode != "" ? $".{languageCode}" : "";
            var subjectFileName = Path.Combine(_env.ContentRootPath, "EmailTemplates", $"{templateName}{languageSuffix}.Subject.txt");
            var bodyFileName    = Path.Combine(_env.ContentRootPath, "EmailTemplates", $"{templateName}{languageSuffix}.Body.html");

            if (!File.Exists(subjectFileName) || !File.Exists(bodyFileName))
            {
                _logger.LogError($"Email template `{templateName}` does not exist");
                return;
            }

            try
            {
                using (var bodyFile = File.OpenRead(bodyFileName))
                    using (var subjectFile = File.OpenRead(subjectFileName))
                    {
                        subject = new StringBuilder(new StreamReader(subjectFile).ReadToEnd());
                        body    = new StringBuilder(new StreamReader(bodyFile).ReadToEnd());
                    }
            }
            catch (IOException e)
            {
                _logger.LogError($"Email template `{templateName}` could not be accessed", e);
                throw;
            }

            foreach (var p in _emailPlaceholders)
            {
                values.Add(p.Key.ToLower(), p.Value);
            }

            foreach (var p in props)
            {
                values.Add(p.Name.ToLower(), p.GetValue(model) as string);
            }

            foreach (var v in values)
            {
                subject.Replace($"[{v.Key}]", v.Value);
                body.Replace($"[{v.Key}]", v.Value);
            }

            await _emailSender.Send(toEmail, toName, subject.ToString(), body.ToString());
        }
Beispiel #12
0
 public MailRequest(IEmailModel model)
 {
     _emailModel = model;
     Subject     = "Umbrella Web App Query";
     Body        = BuildMessageBody();
 }
 public async Task QueueMessageNoOverrideAsync(IEmailModel email, MailAddress recipient)
 {
     await SendEmailAsync(email, recipient, null);
 }