public static MailMessage CreateEmailFromTemplate <T>(string htmlTemplateFile, string textTemplateFile, T model)
            where T : class
        {
            ReplaceTemplate htmlTpl = new ReplaceTemplate(htmlTemplateFile);
            ReplaceTemplate txtTpl  = new ReplaceTemplate(textTemplateFile);
            var             props   = model.GetType().GetProperties().Where(q => q.CanRead);

            foreach (PropertyInfo p in props)
            {
                txtTpl.AddReplacementValue(p.Name, GetPropertyValue(model, p.Name).ToString());
                htmlTpl.AddReplacementValue(p.Name, GetPropertyValue(model, p.Name).ToString());
            }

            MailMessage msg = new MailMessage();

            msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlTpl.GetTemplate(), null, MediaTypeNames.Text.Html));
            msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(txtTpl.GetTemplate(), null, MediaTypeNames.Text.Plain));

            //optional AppSettings values

            SetOptionalDefaults(msg);


            return(msg);
        }
Beispiel #2
0
        /// <summary>
        /// Do File Replace
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            HighLight hl = new HighLight(txtFiles);

            hl.Reset2Default();

            if (txtFiles.Lines.Length == 0)
            {
                txtFiles_DoubleClick(sender, e);
                return;
            }

            if (string.IsNullOrEmpty(cmbPattern.Text))
            {
                MessageBox.Show("Please select a pattern !");
                return;
            }

            ReplaceTemplate pattern = patternLoader[cmbPattern.Text];

            ReplaceFactory.ReplaceFiles(txtFiles.Lines, pattern, hl.Highlight);
        }
        /// <summary>
        /// Generates email message based on a template
        /// </summary>
        /// <typeparam name="T">Model type for template string replacements</typeparam>
        /// <param name="template">path to template file relative to the executable</param>
        /// <param name="model">Model Data for template replacements - only properties in a readable scope are used in replacements(privates are hidden from replacements)</param>
        /// <returns></returns>
        public static MailMessage CreateEmailFromTemplate <T>(string template, T model)
            where T : class
        {
            ReplaceTemplate tpl   = new ReplaceTemplate(template);
            var             props = model.GetType().GetProperties().Where(q => q.CanRead);

            foreach (PropertyInfo p in props)
            {
                tpl.AddReplacementValue(p.Name, GetPropertyValue(model, p.Name).ToString());
            }

            MailMessage msg = new MailMessage {
                Body = tpl.GetTemplate()
            };

            //optional AppSettings values

            SetOptionalDefaults(msg);


            return(msg);
        }
Beispiel #4
0
        public Cmd(string str)
        {
            string[] items = str.Split(new string[] { ";\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            cmdkeyvalue = new Dictionary <string, string>();
            foreach (string s in items)
            {
                if (s.Contains("="))
                {
                    string name = s.Substring(0, s.IndexOf('=')).Trim();
                    if (ValidTools.ValidName(name) && !cmdkeyvalue.ContainsKey(name))
                    {
                        cmdkeyvalue[name] = s.Substring(s.IndexOf('=') + 1);
                    }
                }
            }

            if (cmdkeyvalue.ContainsKey("replacetonull"))
            {
                ReplacetoNullTags = new BETags(cmdkeyvalue["replacetonull"]);
            }
            if (cmdkeyvalue.ContainsKey("addprefix"))
            {
                Prefix = cmdkeyvalue["addprefix"];
            }
            if (cmdkeyvalue.ContainsKey("addsubfix"))
            {
                Subfix = cmdkeyvalue["addsubfix"];
            }

            return;

            ///////////////////////////////
            if (cmdkeyvalue.ContainsKey("nexturl"))
            {
                NextUrlTags = new BETags(cmdkeyvalue["nexturl"]);
            }
            if (cmdkeyvalue.ContainsKey("nextexist"))
            {
                NextExist = cmdkeyvalue["nextexist"];
            }
            if (cmdkeyvalue.ContainsKey("replacetemplate"))
            {
                ReplaceTemplate = cmdkeyvalue["replacetemplate"];
                ReplaceTemplate = ReplaceTemplate.Replace("\\r\\n", "\r\n")
                                  .Replace("\\t", "\t");
            }
            if (cmdkeyvalue.ContainsKey("multisubitem"))
            {
                MultiSubItemTags = new BETags(cmdkeyvalue["multisubitem"]);
            }
            if (cmdkeyvalue.ContainsKey("reversematch"))
            {
                if (cmdkeyvalue["reversematch"] == "true")
                {
                    ReverseMatch = true;
                }
            }
            if (cmdkeyvalue.ContainsKey("txtasurl"))
            {
                if (cmdkeyvalue["txtasurl"] == "true")
                {
                    TxtAsUrl = true;
                }
            }
            if (cmdkeyvalue.ContainsKey("dbidbeginend"))
            {
                BETags bts = new BETags(cmdkeyvalue["dbidbeginend"]);
                Bedbid = null;
                if (bts.tags.Count == 1 && ValidTools.ValidNumber(bts.tags[0].Begin) &&
                    ValidTools.ValidNumber(bts.tags[0].End))
                {
                    Bedbid   = new BEId();
                    Bedbid.B = Convert.ToInt32(bts.tags[0].Begin);
                    Bedbid.E = int.MaxValue;
                    if (ValidTools.ValidNumber(bts.tags[0].End))
                    {
                        Bedbid.E = Convert.ToInt32(bts.tags[0].End);
                    }
                    Bedbid.MoveToTop();
                }//else 未设置BeDbid
            }
            if (cmdkeyvalue.ContainsKey("savepath"))
            {
                SavePath = cmdkeyvalue["savepath"];
            }
            if (cmdkeyvalue.ContainsKey("downloadsuburl"))
            {
                string downloadsuburl = cmdkeyvalue["downloadsuburl"];
                if (downloadsuburl.ToLower().Trim() == "true")
                {
                    DownLoadSuburl     = true;
                    DownLoadSuburlType = "auto";
                    if (cmdkeyvalue.ContainsKey("downloadsuburltype"))
                    {
                        DownLoadSuburlType = cmdkeyvalue["downloadsuburltype"].Trim();
                    }
                }
            }
            if (cmdkeyvalue.ContainsKey("casecmd"))
            {
                BEPos bp = BETag.FormatCmd(cmdkeyvalue["casecmd"], '{', '}');
                if (bp.Valid())
                {
                    string cmdstr = bp.String;
                    caseCmd = new CaseCmd(cmdstr);
                }
            }
            if (cmdkeyvalue.ContainsKey("table"))
            {
                BEPos bp = BETag.FormatCmd(cmdkeyvalue["table"], '{', '}');
                if (bp.Valid())
                {
                    string tablecmdstr = bp.String;
                    tableCmd = new TableCmd(tablecmdstr);
                    ListTable.Add(tableCmd); // 以后可以添加多个Table
                }
            }
            if (cmdkeyvalue.ContainsKey("tablevalue"))
            {
                BEPos bp = BETag.FormatCmd(cmdkeyvalue["tablevalue"], '{', '}');
                if (bp.Valid())
                {
                    string tablecmdstr = bp.String;
                    tablevalueCmd = new TableValueCmd(tablecmdstr);
                }
            }
        }