快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡新京返水_龟发之家论坛



紧接着上一篇文章“ WPF开拓邮件群发对象之实现进度条 ”,本文想把在对象开拓中的实现要点跟大年夜家交流、分享下,首先要说的是这个对象算是个Demo,实现的措施和功能都对照简单,还有不少细节必要完善...

本文目录:

若何随机天生真实存在的QQ邮箱帐号?

单线程的应用及留意

群发对象开拓综述

若何随机天生真实存在的QQ邮箱帐号?

颠末测试,经由过程C#代码发送邮件(调用SmtpClient类的Send措施,此措施无返回值,只会在发送邮箱账户差错或邮箱账户设置有问题,更多的是你用一个发送邮箱账户做群发,由于每种邮箱都有其这方面的限定,在继续很短光阴内,只容许接管同一个邮箱发送的限量的邮件,如:QQ的限定大年夜概50个阁下),纵然给一个根本不存在的邮箱里发送邮件,不会抛出非常,也更无法知道是否已发送成功,只能默觉得发送成功,这样一说,彷佛随机天生真实存在的QQ邮箱帐号是无法实现的事。——着实不然,可以用一个或几个qq邮箱账户检测天生的QQ邮箱是否真实存在! 代码实现如下:

///

/// 天生邮箱的事故处置惩罚///

///

///

void btnGenerate_Email_Click(object sender, RoutedEventArgs e){

#region 判断string str = this.txtGenerateEmailCount.Text.Trim();

if (string.IsNullOrEmpty(str)){

MessageBo澳门新葡新京返水x.Show("请输入要天生的邮箱个数!");return;

}int generate_Email_Count = 0;

int.TryParse(str, out generate_Email_Count);if (generate_Email_Count

{MessageBox.Show("邮箱个数必须为大年夜于0!");

return;}

EmailAccount emailAccount = this._emailAccountList.Find(E => E.Smtp == "smtp.qq.com");if (emailAccount == null)

{MessageBox.Show("qq邮箱账户不存在!");

return;}

#endregionthis._sendEmailsPropertyChanged.PropertyData += this.GetInfoTip("天生QQ邮箱");

EmailInfoEntity emailInfoEntity = new EmailInfoEntity(){

Title = this.txtEmailTitle.Text,Body = this.txtEmailContent.Text,

DisplayName = this.txtDisplayName.Text.Trim(),Attachment = _accessoriesFilePath,

Host = emailAccount.Smtp,From = emailAccount.Email,

Pwd = emailAccount.Pwd};

#region 线程处置惩罚this.StartSingleThreadWork(() =>

{int i = 0, num;

string email = string.Empty;System.Random random = new Random();

bool isSuc;while (i

{num = random.Next(123456, 987654321);

email = string.Format("{0}@qq.com", num);if (this._emailList.Contains(email))

continue;emailInfoEntity.To = email;

isSuc = EmailHelper.SendEmail(emailInfoEntity);Thread.Sleep(1000);

if (!isSuc){

_sendResult.SendFailEmails += string.Format("【{0}】:发送掉败!{1}", email, Environment.NewLine);continue;

}this._emailList.Add(email);

this._sendEmailsPropertyChanged.PropertyData += email + Envir澳门新葡新京返水onment.NewLine;i++;

Console.WriteLine(email);}

MessageBox.澳门新葡新京返水Show("Generate Over");});

#endregion}

单线程的应用及留意

///

/// 开始一个单线程事情///

///

private void StartSingleThreadWork(ThreadStart threadStartHandle)

{_thread = new Thread(() =>

{//禁用发送按钮

_sendResult.SendControlIsEnabled = false;threadStartHandle();

//启用发送按钮_sendResult.SendControlIsEnabled = true;

_thread.Abort();_thread = null;

Console.WriteLine(threadStartHandle.Method.Name);//---此行代码不会履行,由于它位于 线程终止Abort措施之后,即Abort措施看护当火线程的任务已完成,其它的后续事情它澳门新葡新京返水将不会再履行!});

_thread.Start();}

除了上面代码中的注释阐明,还有一点必要重点指出的是:.Net的公用说话运行时(CLR)能区分两种不合类型的线程:前台线程和后台线程。这两者的差别便是:利用法度榜样必须运行完所有的前台线程才可以退出;而对付后台线程,利用法度榜样则可以不斟酌其是否已经运行完毕而直接退出,所有的后台线程在利用法度榜样退出时都邑自动停止。以是,当利用法度榜样的窗体关闭时,你盼望停止所有的线程,你可以把线程设置为后台线程,或在窗体关闭事故中反省是否存在的履行中的线程并对其销毁!

群发对象开拓综述

为了前进邮件发送的射中率,基于上面所提到的问题,应该采纳多个邮箱账户轮发。

核心代码如下:

void btnSend_Click(object sender, RoutedEventArgs e)

{#region 判断

if (this._emailList == null || this._emailList.Count == 0){

MessageBox.Show("当前要发送的邮箱为空!");return;

}string eamilTitle = this.txtEmailTitle.Text.Trim();

if (string.IsNullOrEmpty(eamilTitle)){

MessageBox.Show("请输入email标题!");return;

}string emailContent = this.txtEmailContent.Text.Trim();

if (string.IsNullOrEmpty(emailContent)){

MessageBox.Show("请输入email内容!");return;

}if (!string.IsNullOrEmpty(_accessoriesFilePath))

{if (!System.IO.File.Exists(_accessoriesFilePath))

{MessageBox.Show("请确认所选附件是否存在!");

return;}

}#endregion

string displayName = this.txtDisplayName.Text.Trim();int count = this._emailList.Count;

this.progressBar.Maximum = count;#region 发送处置惩罚

this.StartSingleThreadWork(() =>{

Console.WriteLine("The send count is :" + count);bool isSuc;

int failCount = 0, i = 1;string failTip = string.Empty;

foreach (string email in this._emailList){

_sendResult.CurrentSendNum = i;_sendResult.ProgressBarNumShow = i + "/" + count;

_sendCount = 0;_sendAccountLastIndex = -1;

isSuc = SendEmail(new EmailInfoEntity(){

DisplayName = displayName,Body = emailContent,

Title = eamilTitle,Attachment = _accessoriesFilePath,

To = email});

i++;if (!isSuc)

{failTip = string.Format("【{0}】:发送掉败!{1}", email, Environment.NewLine);

_sendResult.SendResultMes += failTip;_sendResult.SendFailEmails += failTip;

failCount++;}

System.Threading.Thread.Sleep(1000);}

string tipStr = ("failCount is :" + failCount);_sendResult.SendResultMes += tipStr;

MessageBox.Show(tipStr);});

#endregion}

private bool SendEmail(EmailInfoEntity emailInfoEntity)

{if (emailInfoEntity == null)

throw new ArgumentNullException("emailInfoEntity is Null");int sendAccountIndex = GetSendAccountIndex();

EmailAccount emailAccount = _emailAccountList[sendAccountIndex];emailInfoEntity.Host = emailAccount.Smtp;

emailInfoEntity.From = emailAccount.Email;emailInfoEntity.Pwd = emailAccount.Pwd;

bool isSuc = EmailHelper.SendEmail(emailInfoEntity);_sendAccountLastIndex = sendAccountIndex;

_sendResult.SendResultMes += string.Format("★From:{0}★To:{1}★---isSuc:{2}!{3}", emailInfoEntity.From, emailInfoEntity.To, isSuc, Environment.NewLine);_sendCount++;

if (!isSuc){

//假如发送掉败且已发送次数_sendCount小于_emailAccountListCount,则再次发送if (_sendCount

{//当火线程挂起1s,再次发送

System.Threading.Thread.Sleep(1000);return SendEmail(emailInfoEntity);

}}

return isSuc;}

///

/// 获适合前发送邮件的邮箱账户的index///

///private int GetSendAccountIndex()

{if (_emailAccountListCount > 1)

{int sendAccountIndex = new Random().Next(_emailAccountListCount);

if (_sendAccountLastIndex >= 0 && sendAccountIndex == _sendAccountLastIndex)return GetSendAcc澳门新葡新京返水ountIndex();

return sendAccountIndex;}

return 0;}

对象运行效果图如下:

好了,本文到此停止,盼望有这么履历或更好的建议的同伙,能够多多交流!

邮件群发对象.rar

转自:http://www.cnblogs.com/know/archive/2013/03/05/2944307.html

您可能还会对下面的文章感兴趣: