예제 #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public EmailSettingsEditModel(EmailSettings emailSettings)
 {
     if (emailSettings != null)
     {
         AdminEmailAddress = emailSettings.AdminEmailAddress;
         BatchSendLimit = emailSettings.BatchSendLimit;
         NoReplyAddress = emailSettings.NoReplyAddress;
         NumberOfTries = emailSettings.NumberOfTries;
         SendTimeInterval = emailSettings.SendTimeInterval;
         if (emailSettings.SmtpSettings != null)
         {
             Host = emailSettings.SmtpSettings.Host;
             Password = emailSettings.SmtpSettings.Password;
             Port = emailSettings.SmtpSettings.Port;
             UserEmailAddress = emailSettings.SmtpSettings.UserEmailAddress;
             EnableSsl = emailSettings.SmtpSettings.EnableSsl;
             ForceSmtpUserAsFromAddress = emailSettings.SmtpSettings.ForceSmtpUserAsFromAddress;
             UserName = emailSettings.SmtpSettings.UserName;
             RequireCredentials = emailSettings.SmtpSettings.RequireCredentials;
         }
     }
 }
예제 #2
0
        /// <summary>
        /// 生成MailMessage
        /// </summary>
        /// <param name="templateName">邮件模板名称</param>
        /// <param name="model">解析模板使用的数据</param>
        /// <param name="from">发件人</param>
        /// <param name="to">收件人</param>
        /// <param name="cc">抄送地址</param>
        /// <param name="bcc">密送地址</param>
        /// <exception cref="CommonExceptionDescriptor">发件人不能为null</exception>
        /// <exception cref="CommonExceptionDescriptor">编译邮件模板标题时报错</exception>
        /// <exception cref="CommonExceptionDescriptor">编译邮件模板内容时报错</exception>
        /// <exception cref="CommonExceptionDescriptor">邮件模板中Body、BodyUrl必须填一个</exception>
        /// <returns>返回生成的MailMessage</returns>
        public MailMessage Resolve(string templateName, dynamic model, IEnumerable <string> to, string from = null, IEnumerable <string> cc = null, IEnumerable <string> bcc = null)
        {
            if (to == null)
            {
                return(null);
            }
            if (model == null)
            {
                model = new ExpandoObject();
            }
            IEmailSettingsManager emailSettingsManager = DIContainer.Resolve <IEmailSettingsManager>();
            EmailSettings         settings             = emailSettingsManager.Get();
            //if (settings.SmtpSettings.ForceSmtpUserAsFromAddress)
            //    from = settings.SmtpSettings.UserEmailAddress;

            EmailTemplate emailTemplate = GetEmailTemplate(templateName);

            if (string.IsNullOrEmpty(from))
            {
                if (string.Equals(emailTemplate.From, "NoReplyAddress", StringComparison.CurrentCultureIgnoreCase))
                {
                    from = settings.NoReplyAddress;
                }
                else if (string.Equals(emailTemplate.From, "AdminAddress", StringComparison.CurrentCultureIgnoreCase))
                {
                    from = settings.AdminEmailAddress;
                }
                else
                {
                    throw new ExceptionFacade(new CommonExceptionDescriptor("发件人不能为null"));
                }
            }


            MailMessage email = new MailMessage();

            email.IsBodyHtml = true;



            try
            {
                //todo all by bianchx:为了保证发送出去的邮件发送人名称显示都是相同的,所以需要保证Model中的SiteName都是有效的
                email.From = new MailAddress(from, model.SiteName);
            }
            catch { }

            foreach (var toAddress in to)
            {
                try
                {
                    email.To.Add(toAddress);
                }
                catch { }
            }

            if (cc != null)
            {
                foreach (var ccAddress in cc)
                {
                    try
                    {
                        email.CC.Add(ccAddress);
                    }
                    catch { }
                }
            }
            if (bcc != null)
            {
                foreach (var bccAddress in bcc)
                {
                    try
                    {
                        email.Bcc.Add(bccAddress);
                    }
                    catch { }
                }
            }

            //使用RazorEngine解析 EmailTemplate.Subject
            try
            {
                email.Subject = Razor.Parse(emailTemplate.Subject, model, emailTemplate.TemplateName);
            }
            catch (Exception e)
            {
                throw new ExceptionFacade(new CommonExceptionDescriptor("编译邮件模板标题时报错"), e);
            }
            email.Priority = emailTemplate.Priority;
            if (!string.IsNullOrEmpty(emailTemplate.Body))
            {
                try
                {
                    email.Body = Razor.Parse(emailTemplate.Body, model, emailTemplate.Body);
                }
                catch (Exception e)
                {
                    throw new ExceptionFacade("编译邮件模板内容时报错", e);
                }
            }
            else if (!string.IsNullOrEmpty(emailTemplate.BodyUrl))
            {
                email.Body = HttpCollects.GetHTMLContent(emailTemplate.BodyUrl);
            }
            else
            {
                throw new ExceptionFacade("邮件模板中Body、BodyUrl必须填一个");
            }
            return(email);
        }
예제 #3
0
 /// <summary>
 /// 保存EmailSettings
 /// </summary>
 /// <param name="emailSettings"></param>
 public void Save(EmailSettings emailSettings)
 {
     repository.Save(emailSettings);
 }