您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > c#邮件群发系统代码解析
2011/1/14Blog:xiaoyaosr.blog.51cto.com|逍遥散人XIAOYAOSR邮件群发系统代码解析作者:逍遥散人Blog:概要:子线程工作,发送成功邮件做标记,逐条提取数据,气泡提示信息,最小化到托盘,实时进度条代码:usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Threading;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Net.Mail;usingSystem.Net;usingSystem.Text.RegularExpressions;namespaceMailGroupSends{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatestringsubject=string.Empty;privatestringmailto=string.Empty;privatestringbody=string.Empty;privateintsucNum=0;privateintTotalDate{get{if(this.txtDateCount.Text.Trim().Length0)returnConvert.ToInt32(this.txtDateCount.Text.Trim());return4;}set{this.txtDateCount.Text=value.ToString();}}privatestringConnString作者:逍遥散人Blog:{get{stringpathFull=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;stringpath=pathFull.Substring(0,pathFull.LastIndexOf(@\));return@Provider=Microsoft.Jet.OLEDB.4.0;DataSource=+path+@\mail.mdb;}}privateboolmailSent=false;//邮件是否发送成功privateintmailTotalCount=0;privateintCategoryId=0;privateintSentCount=0;//已发送数privateintUnitConversion=1;//默认为秒///summary///发送间隔////summaryprivateintInterval{get{inttimer=0;inttotalMis=(TotalDate*UnitConversion*1000);timer=totalMis/(mailTotalCount-SentCount);returntimer;}}///summary///提取邮件////summaryprivatevoidInitMailList(){OleDbConnectionConn=newOleDbConnection(ConnString);try{stringstrWhere=CategoryId==1000?:andcategoryid=+CategoryId+orderbyisSend,uyx;stringsqlStr=@selecttop1uyxfrom[usermail]whereisDelete=0andIsSend=0+strWhere;if(Conn.State==ConnectionState.Closed)Conn.Open();作者:逍遥散人Blog:=newOleDbCommand(sqlStr,Conn);OleDbDataReaderreader=cmd.ExecuteReader();mailto=string.Empty;while(reader.Read()){mailto+=reader[0].ToString()+,;}reader.Close();if(reader!=null)reader.Dispose();mailto=mailto.Trim(,.ToCharArray());}catch(Exceptionex){MessageBox.Show(ex.Message,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);}finally{if(Conn.State==ConnectionState.Open)Conn.Close();if(Conn!=null)Conn.Dispose();}}///summary///加载邮件列表////summaryprivatevoidLoadMailList(){this.dataGridView1.Columns.Clear();if(this.dataGridView1.Rows.Count0)this.dataGridView1.Rows.Clear();dataGridView1.RowHeadersWidth=21;DataGridViewTextBoxColumncolumn=newDataGridViewTextBoxColumn();column.ReadOnly=true;column.HeaderText=邮箱列表;column.DisplayIndex=0;column.Name=邮箱列表;column.Width=120;dataGridView1.Columns.Add(column);column=newDataGridViewTextBoxColumn();column.ReadOnly=true;column.HeaderText=状态;作者:逍遥散人Blog:=1;column.Name=状态;column.Width=60;dataGridView1.Columns.Add(column);OleDbConnectionConn=newOleDbConnection(ConnString);try{stringstrWhere=CategoryId==1000?:andcategoryid=+CategoryId+orderbyisSend,uyx;stringsql=@selectuyx,IsSendfromusermailwhereisDelete=0+strWhere;if(Conn.State==ConnectionState.Closed)Conn.Open();OleDbCommandcmd=newOleDbCommand(sql,Conn);OleDbDataReaderreader=cmd.ExecuteReader();mailTotalCount=0;while(reader.Read()){DataGridViewRowdgvr=newDataGridViewRow();DataGridViewTextBoxCellcell=newDataGridViewTextBoxCell();cell.Value=reader[0].ToString();dgvr.Cells.Add(cell);cell=newDataGridViewTextBoxCell();cell.Value=●;cell.Style.ForeColor=Convert.ToInt32(reader[1])==0?System.Drawing.Color.Yellow:System.Drawing.Color.Green;dgvr.Cells.Add(cell);dataGridView1.Rows.Add(dgvr);mailTotalCount++;}reader.Close();if(reader!=null)reader.Dispose();this.lblmailCount.Text=(+SentCount+/+mailTotalCount+);}catch{}finally{if(Conn.State==ConnectionState.Open)Conn.Close();if(Conn!=null)Conn.Dispose();}}作者:逍遥散人Blog:已发送邮件个数////summaryprivatevoidSentMailCount(){OleDbConnectionConn=newOleDbConnection(ConnString);try{stringstrWhere=CategoryId==1000?:andcategoryid=+CategoryId;stringsqlStr=@selectuyxfrom[usermail]whereisDelete=0andIsSend=1+strWhere;if(Conn.State==ConnectionState.Closed)Conn.Open();OleDbDataAdapteradapter=newOleDbDataAdapter(sqlStr,Conn);DataSetds=newDataSet();adapter.Fill(ds);SentCount=ds.Tables[0].Rows.Count;}catch(Exceptionex){MessageBox.Show(ex.Message,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);}finally{if(Conn.State==ConnectionState.Open)Conn.Close();if(Conn!=null)Conn.Dispose();}}///summary///标记出错的邮箱////summary///paramname=mail/paramprivatevoidMarkErrorMail(stringmail){OleDbConnectionConn=newOleDbConnection(ConnString);try{stringsql=updateusermailsetIsDelete=10whereuyx='+mail+';if(Conn.State==ConnectionState.Closed)Conn.Open();OleDbCommandcmd=newOleDbCommand(sql,Conn);cmd.ExecuteNonQuery();}catch作者:逍遥散人Blog:{}finally{if(Conn.State==ConnectionState.Open)Conn.Close();if(Conn!=null)Conn.Dispose();}}///summary///修改已发送邮件状态////summary///paramname=mail/paramprivate
本文标题:c#邮件群发系统代码解析
链接地址:https://www.777doc.com/doc-5731818 .html