Exemple #1
0
        /// <summary>
        /// Sends member emails by type.
        /// </summary>
        /// <param name="emailType">Type of the email.</param>
        /// <param name="member">The member.</param>
        public void SendUserEmail(MemberEmailType emailType, Member member)
        {
            var configuration = _mailConfiguration.GetConfiguration();

            if (configuration.MailingEnabled)
            {
                try
                {
                    var templateConfiguration = configuration.Templates.First(t => t.Type == emailType.ToString());
                    var html    = Razor.Run(emailType.ToString(), member);
                    var subject = templateConfiguration.Subject;
                    var from    = configuration.DefaultMailingAddress;
                    var to      = member.EmailAddress;

                    var message = new MailMessage(from, to, subject, html)
                    {
                        IsBodyHtml = true
                    };
                    message = ReplaceMemberValueTokens(message, member);

                    SendMail(message);
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message, ex);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Sends member emails by type.
        /// </summary>
        /// <param name="emailType">Type of the email.</param>
        /// <param name="userId">The user id.</param>
        public void SendUserEmail(MemberEmailType emailType, int userId)
        {
            var configuration = _mailConfiguration.GetConfiguration();

            if (configuration.MailingEnabled)
            {
                var mailType     = int.Parse(emailType.ToString("d"));
                var mailTemplate = _emailTemplateRepository.Get(e => e.EmailTemplateId == mailType).FirstOrDefault();
                if (mailTemplate == null)
                {
                    return;
                }

                var user    = _memberRepository.Get(m => m.MemberId == userId).FirstOrDefault();
                var subject = mailTemplate.EmailSubject;
                var body    = mailTemplate.EmailBody;
                var from    = configuration.DefaultMailingAddress;
                var to      = user.EmailAddress;

                var valuesPropertyBag = new MemberEmailPropertyBag(userId);

                body    = valuesPropertyBag.Keys.Aggregate(body, (current, token) => current.Replace(string.Format("{0}", token), valuesPropertyBag[token]).Replace("{~", "").Replace("}", ""));
                subject = valuesPropertyBag.Keys.Aggregate(subject, (current, token) => current.Replace(string.Format("{0}", token), valuesPropertyBag[token]).Replace("{~", "").Replace("}", ""));

                var message = new MailMessage(from, to, subject, body)
                {
                    IsBodyHtml = true
                };
                var mailClient = new SmtpClient(configuration.Host, configuration.Port.Value);

                try
                {
                    mailClient.Send(message);
                }
                catch (Exception ex)
                {
                }
            }
        }