作者c邮件群发系统代码解析

邮件群发系统  时间:2021-01-31  阅读:()

概要

子线程工作发送成功邮件做标记逐条提取数据气泡提示信息最小化到托盘实时进度条

代码us ing System;us ing System.Collections;us ing System.Collections.Generic;using System.ComponentModel;using System.Data;us ing System.Data.OleDb;us ing System.Threading;using System.Text;using System.Windows.Forms;us ing System.Net.Mail;us ing System.Net;us ing System.Text.RegularExpress ions;names pace M ailGroup Sends

{public partial class Form1 :Form

{public Form1()

{

Initialize Comp onent();

}private string subject=string.Empty;private string mailto=string.Empty;private string body=string.Empty;private int sucNum=0;private int TotalDate

{get

{if(this.txtDateCount.Text.Trim().Length>0)return Convert.ToInt32(this.txtDateCount.Text.Trim());return 4;

}set { this.txtDateCount.Text=value.ToString(); }

}private string ConnString

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

{get

{string pathFull=

Sys tem.Diagn ostics.Pro ces s.GetCurrentPro ces s().M ainModu le.FileName;string path=pathFull.Substring(0,pathFull.LastIndexOf(@"\"));return@"Provider=Micros oft.Jet.OLEDB.4.0;Data Source="+path+@"\mail.mdb";

}

}private bool mailSent=false; //邮件是否发送成功private int mailTotalCount=0;private int CategoryId=0;private int SentCount=0; //已发送数private int UnitConversion=1; //默认为秒

///<summary>

///发送间隔

///</summary>private int Interval

{get

{int timer=0;int totalMis=(TotalDate*UnitConvers ion* 1000);timer=to talM is / (mailTo talCount-S entCount);return timer;

}

}

///<summary>

///提取邮件

///</summary>private void InitMailList()

{

O leDbConnection Conn=new OleDbConnection(ConnString);try

{string strWhere=CategoryId==1000? "" : "and categoryid="+CategoryId+"orderby isSend,uyx";string sqlStr=@"select top 1 uyxfrom[usermail]where isDelete=0 and IsSend=0"+strWhere;if(Conn.State==ConnectionState.Clos ed)Conn.Open();

OleDb Command cmd=new OleDb Command(s qlStr,Conn);

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

OleDbDataReader reader=cmd.ExecuteReader();mailto=string.Empty;wh ile(re ad er.Re ad())

{mailto+=reader[0].To String()+",";

}reader.Clos e();if(reader!=null)reader.Dispos e();mailto=mailto.Trim(",".ToCharArray());

}catch(Exception ex)

{

Mess ageBox.Show(ex.Mess age,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);

}fin a lly

{if(Conn.State==Connection State.Open)Conn.Clo se();if(Conn!=null)Conn.Disp ose();

}

}

///<summary>

///加载邮件列表

///</summary>private void LoadMailList()

{this.dataGridView 1.Co lumn s.Clear();if(this.dataGridView 1.Rows.Count>0)this.dataGridView 1.Rows.Cle ar();dataGridView1.RowHeadersWidth=21;

DataGridViewTextBo xColumn column=new DataGridViewTextBo xColumn();column.ReadOnly=true;column.HeaderText="邮箱列表";column.DisplayIndex=0;column.Name="邮箱列表";column.Width=120;dataGrid View1.Columns.Add(column);column=new DataGridViewTextBo xColumn();column.ReadOnly=true;column.HeaderText="状态";column.DisplayIndex=1;

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

dataGrid View1.Columns.Add(column);

O leDbConnection Conn=new OleDbConnection(ConnString);try

{string strWhere=CategoryId==1000?"" : "and categoryid="+CategoryId+"orderby isSend,uyx";string sql=@"select uyx,Is Send fromusermail where isDelete=0"+strWhere;if(Conn.State==ConnectionState.Clos ed)Conn.Open();

OleDbCommand cmd=new OleDbCommand(s ql,Conn);

OleDbDataReader reader=cmd.ExecuteReader();mai lTo talCo unt=0;wh ile(re ad er.Re ad())

{

DataGridViewRow dgvr=new DataGridViewRow();

DataGridViewTextBo xCell cell=new DataGridViewTextBo xCe ll();cell.Value=reader[0].To String();dgvr.Ce lls.A dd(c e ll);cell=new DataGridViewTextBo xCell();cell.Value="●";c ell.Style.ForeCo lor=Convert.To Int32(reader[1])==0?

Sys tem.Drawin g.Co lo r.Ye l lo w:Sy s t em.Drawin g.Co lo r.Gre en;dgvr.Ce lls.A dd(c e ll);dataGridView 1.Rows.Add(dgvr);ma ilT o t a lCo un t++;

}reader.Clos e();if(reader!=null)reader.Dispos e();this.lb lmai lCount.Text=" "+SentCount+"/"+mailTotalCount+" ";

}catch

{

}fin a lly

{if(Conn.State==Connection State.Open)Conn.Clo se();if(Conn!=null)Conn.Dispose();

}

}

///<summary>

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

///已发送邮件个数

///</summary>private void SentMailCount()

{

O leDbConnection Conn=new OleDbConnection(ConnString);try

{string strWhere=CategoryId==1000? "" : "and categoryid="+CategoryId;string sqlStr=@"select uyxfrom[usermail]where isDelete=0 and IsSend=1 "+strWhere;if(Conn.State==ConnectionState.Clos ed)Conn.Open();

OleDbDataAdapter adapter=new OleDbDataAdapter(sqlStr,Conn);

DataSet ds=new DataSet();a d ap te r.F ill(d s);

SentCount=ds.Tab les[0].Ro ws.Count;

}catch(Exception ex)

{

Mess ageBox.Show(ex.Mess age,"错误",MessageBoxButtons.OK,

MessageBoxIcon.Error);

}fin a lly

{if(Conn.State==Connection State.Open)Conn.Clo se();if(Conn!=null)Conn.Dispose();

}

}

///<summary>

///标记出错的邮箱

///</summary>

///<p aram name="mail"></p aram>private void MarkErrorMail(string mail)

{

O leDbConnection Conn=new OleDbConnection(ConnString);try

{string sql="update usermail set IsDelete=10 where uyx='"+mail+"' ";if(Conn.State==ConnectionState.Clos ed)Conn.Open();

OleDb Command cmd=new OleDbCommand(s ql,Conn);cmd.ExecuteNonQuery();

}catch

{ }

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

{if(Conn.State==Connection State.Open)Conn.Clo se();if(Conn!=null)Conn.Disp ose();

}

}

///<summary>

///修改已发送邮件状态

///</summary>

///<p aram name="mail"></p aram>private void UpdateMailState(string mail, int state)

{

O leDbConnection Conn=new OleDbConnection(ConnString);try

{string where=string.Empty;if(ma il.Length>0)

{

WriteToTxt(DateTime.Now+" Success "+mail, txtLogPath);if(ma il.In d e xO f(",")>-1)'")+"' ";where="and uyx='"+mail+"'";

}e ls e

{where="and IsSend=1";

}string category=CategoryId==1000? "" : "and categoryid="+CategoryId;string sql="update usermail set Is Send="+state+"where IsDelete=0"+category+wh e re;if(Conn.State==ConnectionState.Clos ed)Conn.Open();

OleDb Command cmd=new OleDbCommand(s ql,Conn);cmd.ExecuteNonQuery();

}catch

{ }fin a lly

{if(Conn.State==Connection State.Open)Conn.Clo se();if(Conn!=null)Conn.Disp ose();

}

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

}

///<summary>

///邮件发送

///</summary>

///<p aram n ame="mailTo"></p aram>

///<p aram name="subject"></p aram>

///<p aram name="body"></param>

///<re tu rns></re tu rns>private bool Send(string mailTo, string subject,string body)

{try

{if(!Is Ema il(mailTo))

{

WriteToTxt(DateTime.Now+" Faild "+mailTo, txtLogPath+"Error Message:邮箱格式不正确");

M arkErro rM ail(mai lT o);return fals e;

}if(ma ilTo.Substring(mailTo.Length- 1)=="." | |mai lTo.Substring(mailTo.Length- 1)==",")

{

WriteToTxt(DateTime.Now+" Faild "+mailTo, txtLogPath+"Error Message:邮箱格式不正确");

M arkErro rM ail(mai lT o);return fals e;

}

MailMessage msg=new MailMessage();ms g.From=new MailAddress("***@****","xiaoyaos r",Enco ding.UTF8);if(ma ilT o.In d e xO f(",")>-1)ms g.Bc c.Add(mailTo);e ls ems g.To.A dd(mailTo);msg.Subject=subject;ms g.Body=body;

ArrayList annexList=ReadTxt(txtAnnexPath);for(int i=0; i<annexList.Count; i++)

{msg.Attachments.Add(new Attachment(annexList[i].ToString()));

}

S mtpClient s mtp=new SmtpClient("mail.163.com");

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

s mtp.Credentials=new NetworkCredential("***@****", "*******");smtp.Send(msg);mailSent=true;sucNum++;

}catch(Exception ex)

{if(ex.Mess age.IndexOf("http://www.ipmotor.com/smtp_err.htm")>-1)

{

WriteToTxt(DateTime.Now+" Faild Error Message:"+ex.Message,txtLogPath);this.notifyIcon 1.ShowBallo onTip(Interval, "",ex.Me ssage,Too lTip Icon.None);

System.Threading.Thread.Sleep(Interval*2);base.Dispose(true);

App lication.ExitThread();

Ki llTh re a d();

}

WriteToTxt(DateTime.Now+" Faild "+mailTo+" Error Message:"+ex.Message, txtLogPath);

M ark Erro rM a il(ma ilT o);mailSent=false;

}return mailSent;

}

//*******************************************************************//

//开始发送public void StartMailSend()

{

MailSend mailSend=new MailSend();ma i lS e n d.iT ot a lCount=ma ilT o t a lCo unt;ma i lS e n d.int e rva l=In t e rva l;mailSend.iSentCount=SentCount;mai lSend.onM ailSendProgres s+=new

M ailS end.dM ailS endProgres s(mailS en d_o nM ailS endProgres s);mailSend.Start();

}

//同步更新void mailSend_onMailSendProgress(int total, int current)

{try

作者逍遥散人 Blog:http://xiaoyaosrblog 51cto com

王小玉网-美国洛杉矶2核4G 20元/月,香港日本CN2 2核2G/119元/季,美国300G高防/80元/月!

 活动方案:美国洛杉矶 E5 2696V2 2核4G20M带宽100G流量20元/月美国洛杉矶E5 2696V2 2核4G100M带宽1000G流量99元/季香港CN2 E5 2660V2 2核2G30M CN2500G流量119元/季日本CN2E5 2660 2核2G30M CN2 500G流量119元/季美国300G高防 真实防御E5 2696V2 2核2G30M...

CloudServer:$4/月KVM-2GB/50GB/5TB/三个数据中心

CloudServer是一家新的VPS主机商,成立了差不多9个月吧,提供基于KVM架构的VPS主机,支持Linux或者Windows操作系统,数据中心在美国纽约、洛杉矶和芝加哥机房,都是ColoCrossing的机器。目前商家在LEB提供了几款特价套餐,最低月付4美元(或者$23.88/年),购买更高级别套餐还能三个月费用使用6个月,等于前半年五折了。下面列出几款特别套餐配置信息。CPU:1cor...

Hostodo:4款便宜美国vps七折优惠低至$13/年;NVMe阵列1Gbps带宽,免费DirectAdmin授权

hostodo怎么样?快到了7月4日美国独立日,hostodo现在推出了VPS大促销活动,提供4款Hostodo美国独立日活动便宜VPS,相当于7折,低至$13/年,续费同价。Hostodo美国独立日活动结束时间不定,活动机售完即止。Hostodo商家支持加密数字货币、信用卡、PayPal、支付宝、银联等付款。Hostodo美国独立日活动VPS基于KVM虚拟,NVMe阵列,1Gbps带宽,自带一个...

邮件群发系统为你推荐
免费个人网站制作怎样制作个人网站啊?要免费的!滚筒洗衣机和波轮洗衣机哪个好滚筒洗衣机和波轮洗衣机有什么不同少儿英语哪个好少儿英语,那个好一些?朱祁钰和朱祁镇哪个好历史上真实的明英宗是怎么样的?性格之类的。朱祁钰和朱祁镇的相关的最好可以详细点的浏览器哪个好用哪个浏览器比较好浮动利率和固定利率哪个好浮动利率房贷与固定利率房贷比较 购房者如何选择英语词典哪个好英语词典哪个好美国国际集团IDG在美国是干什么的?51空间登录手机怎么登陆51空间啊扣扣空间登录QQ空间怎么老是提示登陆?
独立ip主机 备案未注册域名 传奇服务器租用 过期备案域名查询 最便宜虚拟主机 服务器配置技术网 BWH mediafire下载工具 wordpress技巧 sockscap 网站监控 线路工具 发包服务器 嘉洲服务器 数字域名 tna官网 空间合租 吉林铁通 双12 带宽租赁 更多