通常我们经常配置一些信息在配置文件和XML中,在不修改程序的前提下修改配置文件或者XML就可以实现。
案列通过邮件中转人发送邮件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
namespace DzOpenPlat.Core
{
/// <summary>
/// MailSend 的摘要说明
/// 作者:龚德辉
/// 日期:2012-08-03
/// </summary>
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; } }
/// <summary>
/// 创建Mail,填入发送人,主题,內容
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 得到配置文件
/// </summary>
/// <param name="Item"></param>
/// <returns></returns>
public string getConfigParamvalue(string Item)
{
return string.Empty;
}
/// <summary>
/// 读xxx.config取配置文件
/// </summary>
/// <param name="Target"></param>
/// <returns></returns>
public string GetConfigValue(string Target)
{
string path = HttpContext.Current.Server.MapPath(configPath);
return GetConfigValue(Target, path);
}
/// <summary>
/// 读xxx.config取配置文件
/// </summary>
/// <param name="Target"></param>
/// <param name="ConfigPathName"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 返回该节点下的此属性的值。
/// </summary>
/// <param name="Target"></param>
/// <param name="strattrbute"></param>
/// <returns></returns>
public string GetConfigAttrbute(string Target, string strattrbute)
{
string path = HttpContext.Current.Server.MapPath(configPath);
return GetConfigValue(Target, path, strattrbute);
}
}
}