?? 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 + -