📄 public.cs
字号:
using System;
using System.Data;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
namespace HBJ.MailSender
{
/// <summary>
/// Public 的摘要说明。
/// </summary>
public class Public
{
public Public()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// Txt文档路径
/// </summary>
public static string txtFile = "";
/// <summary>
/// 是否检查邮址是否重复
/// </summary>
public static bool isCheck = false;
//系统状态信息
public static string statusInfo = "";
/// <summary>
/// 任务列表
/// </summary>
public static DataSet taskDS = null;
/// <summary>
/// 构建系统配置表
/// </summary>
/// <returns></returns>
public static DataTable BuildSystemConfig(){
DataTable table = new DataTable("SystemConfig");
DataColumn cl = new DataColumn("SqlServer");
cl.DataType = System.Type.GetType("System.String");
table.Columns.Add(cl);
cl = new DataColumn("SqlDatabaseName");
cl.DataType = System.Type.GetType("System.String");
table.Columns.Add(cl);
cl = new DataColumn("SqlUsername");
cl.DataType = System.Type.GetType("System.String");
table.Columns.Add(cl);
cl = new DataColumn("SqlPassword");
cl.DataType = System.Type.GetType("System.String");
table.Columns.Add(cl);
cl = new DataColumn("MailServerIsCheck");
cl.DataType = System.Type.GetType("System.String");
table.Columns.Add(cl);
cl = new DataColumn("SendSpaceTime");
cl.DataType = System.Type.GetType("System.String");
table.Columns.Add(cl);
return table;
}
/// <summary>
/// 保存DataTable中的数据到XML
/// </summary>
/// <returns></returns>
public static void SaveDataTableToXML(DataTable table,string path){
if(File.Exists(path)){
File.Delete(path);
}
DataSet ds = new DataSet();
ds.Tables.Add(table.Copy());
Public.CreateXmlFile(ds,path);
}
/// <summary>
/// 创建指定路径的Xml文件
/// </summary>
/// <param name="path"></param>
public static void CreateXmlFile(DataSet ds,string path){
StreamWriter sw = null;
try{
sw = new StreamWriter(path);
ds.WriteXml(sw);
sw.Close();
}catch{
if(sw != null) sw.Close();
}finally{
if(sw != null) sw.Close();
}
}
/// <summary>
/// 读取XML内容到DataTable
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static DataTable ReadDataTaBleFromXML(string path){
if(File.Exists(path)){
DataSet ds = Public.ReadXmlFile(path);
if(ds != null && ds.Tables.Count == 1) return Public.ReadXmlFile(path).Tables[0];
else return null;
}else{
return null;
}
}
/// <summary>
/// 读取指定路径的Xml文件并创建相应的DataSet
/// </summary>
/// <param name="path"></param>
public static DataSet ReadXmlFile(string path){
try{
DataSet ds = new DataSet();
ds.ReadXml(path);
return ds;
}catch(Exception ex){
return null;
}
}
/// <summary>
/// 从TXT文档中读取MAIL保存到SQL中
/// </summary>
/// <param name="txtFile"></param>
public void ReadMailFromToDb(){
//读取TXT文本内容
FileStream fs = new FileStream (Public.txtFile,FileMode.Open,FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs);
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin);
//从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
string strLine = m_streamReader.ReadLine();
while(strLine != null){
string pare = @"^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$";
System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(pare);
if(strLine.Length > 0 && regx.IsMatch(strLine)){
try{
//检查是否需要重复性检查,这样会阡低系统性能
if(Public.isCheck){
if(!Database.MailIsSame(strLine)) Database.SaveMailAddressToList(strLine);
}else Database.SaveMailAddressToList(strLine);
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
}
}
strLine = m_streamReader.ReadLine();
}
//关闭此StreamReader对象
m_streamReader.Close();
MessageBox.Show("导入数据库操作完成!");
}
/// <summary>
/// 将地址加入到列表中
/// </summary>
/// <param name="mailAddress"></param>
/// <param name="?"></param>
public static void InsertMailToListView(string mailAddress,ListView lv){
lv.Items.Add(new ListViewItem(new string[]{mailAddress}));
}
/// 发送邮件
///
/// 返回值为布尔型,判断发送是否成功
public static bool SendMailByJmail(string fromMail,string fromName,string toMail,string toName,string title,string body,string mailServer,string username,string password) {
try {
myJmail.MessageClass myMail = new myJmail.MessageClass();
myMail.Charset="GB2312";//邮件使用字符集
//myMail.Silent = true;
myMail.ContentType = "text/html";
myMail.From = fromMail; //邮件发送者邮件地址
myMail.FromName = fromName; //邮件发送者名称
myMail.AddRecipient(toMail,toName,"");//添加邮件接收者名称以及邮件地址
myMail.Subject = title; //邮件主题
myMail.Body = "<html><head></head><body bgcolor=#EAEAEA>" + body + "</body></html>"; //邮件内容
myMail.MailServerUserName = username; //登陆邮件服务器的用户名
myMail.MailServerPassWord = password; //登陆邮件服务器的密码
myMail.Priority = 3;
myMail.MimeVersion = "1.0";
myMail.ReplyTo = "sales@yedman.com";
myMail.SimpleLayout = true;
bool result = myMail.Send(mailServer,false); //邮件服务器地址(例:smtp.163.com)
if(!result) throw new Exception(myMail.ErrorMessage);
return result;
}catch(Exception ex) {
throw new Exception(ex.ToString());
}
}
public static void SendMailBySMTP(string fromMail,string toMail,string title,string body,string mailServer,string username,string password) {
try {
SMTP_ smtp = new SMTP_(mailServer,56);
//发送
MailMessage mail = new MailMessage();
mail.MailBody = Encoding.Default.GetBytes("<html><head></head><body>" + body + "</body></html>");
mail.Subject = title;
System.Collections.Specialized.StringCollection reces = new System.Collections.Specialized.StringCollection();
mail.Receivers.Add(toMail);
mail.Sender = fromMail;
smtp.UserID = username;
smtp.Password = password;
smtp.SendMail(mail);
if(smtp.ErrorMessage != null && smtp.ErrorMessage.Length > 0)
throw new Exception(smtp.ErrorMessage);
}catch(Exception ex) {
throw new Exception(ex.ToString());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -