通常我们经常配置一些信息在配置文件和XML中,在不修改程序的前提下修改配置文件或者XML就可以实现。
案列通过邮件中转人发送邮件:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; namespace DzOpenPlat.Core { ////// MailSend 的摘要说明 /// 作者:龚德辉 /// 日期:2012-08-03 ///public class MailSend { public MailSend() { } private string mailto; private string mailsubject; private string mailbody; private string attech; public string Mailto { set { mailto = value; } } public string Mailsubject { set { mailsubject = value; } } public string Mailbody { set { mailbody = value; } } public string Attech { set { attech = value; } } ////// 创建Mail,填入发送人,主题,內容 //////public string Send() { try { //处理多个收件人 MailMessage mlMsg = new MailMessage(); string[] toArray = mailto.Split(','); foreach (string i in toArray) { mlMsg.To.Add(new MailAddress(i)); } mlMsg.Subject = mailsubject; mlMsg.Body = mailbody; mlMsg.IsBodyHtml = true; BaseConfig bc = new BaseConfig("~/Config/SmtpSetting.config"); mlMsg.From = new MailAddress(bc.GetConfigValue("UserName"), bc.GetConfigValue("AuthorName")); SmtpClient smtpClient = new SmtpClient(bc.GetConfigValue("SmtpServer")); smtpClient.Credentials = new NetworkCredential(bc.GetConfigValue("UserName"), bc.GetConfigValue("Pwd")); smtpClient.Timeout = 4000; smtpClient.EnableSsl = false; smtpClient.Send(mlMsg); return "发送成功"; } catch (Exception e) { return e.ToString(); } } } }
BaseConfig:
上下左右
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Web; namespace DzOpenPlat.Core { public class BaseConfig { public string configPath= string.Empty; public BaseConfig(string configPath) { this.configPath = configPath; } ////// 得到配置文件 /////////public string getConfigParamvalue(string Item) { return string.Empty; } ////// 读xxx.config取配置文件 /////////public string GetConfigValue(string Target) { string path = HttpContext.Current.Server.MapPath(configPath); return GetConfigValue(Target, path); } ////// 读xxx.config取配置文件 ////////////internal string GetConfigValue(string Target, string XmlPath) { System.Xml.XmlDocument xdoc = new XmlDocument(); xdoc.Load(XmlPath); XmlElement root = xdoc.DocumentElement; XmlNodeList elemList = root.GetElementsByTagName(Target); try { return elemList[0].InnerText; } catch { return null; } } internal string GetConfigValue(string Target, string XmlPath, string strattrbute) { System.Xml.XmlDocument xdoc = new XmlDocument(); xdoc.Load(XmlPath); XmlElement root = xdoc.DocumentElement; XmlNodeList elemList = root.GetElementsByTagName(Target); try { return elemList[0].Attributes[strattrbute].ToString(); } catch { return null; } } ////// 返回该节点下的此属性的值。 ////////////public string GetConfigAttrbute(string Target, string strattrbute) { string path = HttpContext.Current.Server.MapPath(configPath); return GetConfigValue(Target, path, strattrbute); } } }