变量:
#region 变量
private XmlDocument xmlDoc = new XmlDocument();
private ArrayList toList = new ArrayList();
private byte key = 123;//加密字
#region 发件人信息
private string FJRYXQC = string.Empty;
private string FJRYXMM = string.Empty;
private string FJRYXMC = string.Empty;
private string YXFWQDZ = string.Empty;
private string FJRXM = string.Empty;
private string YXFWQPORT = string.Empty;
#endregion
#region 收件人信息
string ACCYXQC = string.Empty;
string ACCBZ = string.Empty;
#endregion
#endregion
发送邮件:
#region 发送邮件
/// <summary>
///
/// </summary>
/// <param name="theme">邮件主题</param>
/// <param name="body">邮件内容</param>
/// <param name="FJLJ">附件路径(附件以字符串的形式存在数组里面)</param>
public void Send(string theme, string body, string[] FJLJ)
{
if (LoadFsrXmlData() && LoadJsrXmlData())
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(FJRYXQC, FJRXM);
//发送给谁
foreach (String to in toList)
{
mail.To.Add(to);
}
//标题
mail.Subject = theme;
//内容编码
mail.BodyEncoding = Encoding.Default;
//发送优先级
mail.Priority = MailPriority.High;
//邮件内容
mail.Body = body;
//是否HTML形式发送
mail.IsBodyHtml = true;
//附件
try
{
Attachment data;
ContentDisposition disposition;
if (FJLJ != null && FJLJ.Length > 0)
{
foreach (string str in FJLJ)
{
data = new Attachment(str, MediaTypeNames.Application.Octet);//实例化附件
disposition = data.ContentDisposition;
mail.Attachments.Add(data);//添加到附件中
}
}
}
catch (Exception ex)
{
throw new Exception("添加附件错误!" + ex.Message);
}
//邮件服务器和端口号
int port = int.Parse(YXFWQPORT);
SmtpClient smtp = new SmtpClient(YXFWQDZ, port);
//587
smtp.UseDefaultCredentials = true;
//指定发送方式
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//指定登录名和密码
smtp.Credentials = new System.Net.NetworkCredential(FJRYXMC, FJRYXMM);
//超时时间
smtp.Timeout = 300000;
smtp.Send(mail);
MessageBox.Show("发送成功!");
}
else
{
if (!LoadFsrXmlData()) {
MessageBox.Show("无发件人信息!");
}
if (!LoadJsrXmlData())
{
MessageBox.Show("无收件人信息!");
}
}
}
#endregion
加载fsr.xml文件 读取发送人xml配置文件:
#region 加载fsr.xml文件 发送人xml配置文件
public bool LoadFsrXmlData()
{
string outMsg = string.Empty;
try
{
string path = Application.StartupPath;
string folder = System.IO.Path.Combine(path, "config");
string xmlFileName = System.IO.Path.Combine(folder, "fsr.xml");
bool folderExists = System.IO.Directory.Exists(folder);
if (folderExists && File.Exists(xmlFileName))//当文件存在时
{
xmlDoc.Load(xmlFileName);
XmlNode root = xmlDoc.SelectSingleNode("root");
XmlNodeList nodeList = root.ChildNodes;//获得root节点的子节点
foreach (XmlElement xe in nodeList)
{
FJRYXQC = xe.GetElementsByTagName("FJRYXQC")[0].InnerText;
FJRYXMM = encryptDecryptStr(xe.GetElementsByTagName("FJRYXMM")[0].InnerText);
FJRYXMC = xe.GetElementsByTagName("FJRYXMC")[0].InnerText;
YXFWQDZ = xe.GetElementsByTagName("YXFWQDZ")[0].InnerText;
FJRXM = xe.GetElementsByTagName("FJRXM")[0].InnerText;
YXFWQPORT = xe.GetElementsByTagName("YXFWQPORT")[0].InnerText;
}
return true;
}
return false;
}
catch (System.Exception ex)
{
MessageBox.Show("加载config.xml文件失败:" + ex.Message);
return false;
}
}
#endregion
加载jsr.xml文件 读取接收人xml配置文件:
#region 加载jsr.xml文件 接收人xml配置文件
public bool LoadJsrXmlData()
{
try
{
string path = Application.StartupPath;
string folder = System.IO.Path.Combine(path, "config");
string xmlFileName = System.IO.Path.Combine(folder, "jsr.xml");
bool folderExists = System.IO.Directory.Exists(folder);
if (folderExists && File.Exists(xmlFileName))//当文件存在时
{
xmlDoc.Load(xmlFileName);
XmlNode root = xmlDoc.SelectSingleNode("root");
XmlNodeList nodeList = root.ChildNodes;//获得root节点的子节点
foreach (XmlElement xe in nodeList)
{
toList.Add(xe.GetElementsByTagName("ACCYXQC")[0].InnerText);
}
return true;
}
return false;
}
catch (System.Exception ex)
{
MessageBox.Show("加载config.xml文件失败:" + ex.Message);
return false;
}
}
#endregion
密码加密:
# region 异或加密算法
public string encryptDecryptStr(string p)
{
byte[] bs = Encoding.Default.GetBytes(p);
for (int i = 0; i < bs.Length; i++)
{
bs[i] = (byte)(bs[i] ^ key);
}
return Encoding.Default.GetString(bs);
}
#endregion