- 浏览: 73626 次
- 性别:
- 来自: 北京
文章分类
最新评论
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mail;
namespace ConsoleApplication1
{
public delegate void CallBack(bool state, int count);
public class Program
{
public void Show(bool state,int count)
{
Console.WriteLine("{0}-{1}",state,count);
}
static void Main(string[] args)
{
int tempCount = 120;
int tempWhileCount = (tempCount / 100) * 100;
int tempOtherCount = tempCount - tempWhileCount;
Console.WriteLine(tempWhileCount);
Console.WriteLine(tempOtherCount);
//指定线程数
int size = 10;
size = tempWhileCount==0?(0):(tempWhileCount <= 100 ? (10) : (tempWhileCount <= 1000 ? (20) : (25)));
List<MailInfo> lall = new List<MailInfo>();
//添加邮件信息
for (int i = 0; i <tempCount; i++)
{
lall.Add(new MailInfo("三毛"+ i.ToString(),"wangdetian16" + i.ToString() + "@qq.com"));
}
//创建多线程
for (int i = 0; i < size; i++)
{
ThreadData temptd = new ThreadData(new CallBack(new Program().Show));
List<MailInfo> tempLi = new List<MailInfo>();
for (int j = i * (tempWhileCount / size); j < (i * (tempWhileCount / size) + tempWhileCount / size); j++)
{
tempLi.Add(lall[j]);
}
temptd.LisMails = tempLi;
Thread tempThread = new Thread(new ThreadStart(temptd.Run));
tempThread.Start();
}
ThreadData temptdOther = new ThreadData(new CallBack(new Program().Show));
List<MailInfo> tempLiOther = new List<MailInfo>();
for (int j = tempWhileCount; j < tempCount; j++)
{
tempLiOther.Add(lall[j]);
}
temptdOther.LisMails = tempLiOther;
Thread tempThreadOther = new Thread(new ThreadStart(temptdOther.Run));
tempThreadOther.Start();
//ThreadData thdata1 = new ThreadData();
//List<string> l1 = new List<string>();
//for (int i = 0; i < 1000; i++)
//{
// l1.Add("wangdetian16"+i.ToString()+"@163.com");
//}
//thdata1.LisMails = l1;
//Thread thread = new Thread(new ThreadStart(thdata1.Run));
//thread.Start();
//ThreadData thdata2 = new ThreadData();
//List<string> l2 = new List<string>();
//for (int i = 0; i < 10; i++)
//{
// l2.Add("wangdetian18" + i.ToString() + "@163.com");
//}
//thdata2.LisMails = l2;
//Thread thread1 = new Thread(new ThreadStart(thdata2.Run));
//thread1.Start();
}
}
public class ThreadData
{
public static string MailMessage = "{0}你好你人物要做近三分萨拉夫接撒立法精神多了几分当上了副驾驶的发生大风蓝色队服那里公司发展规划的";
private List<MailInfo> _LisMails;
public static string frommail = "wangdetian116688@163.com";//System.Configuration.ConfigurationManager.AppSettings["frommail"];
public static string username = "wangdetian116688";//System.Configuration.ConfigurationManager.AppSettings["username"];
public static string password = "987654321";//System.Configuration.ConfigurationManager.AppSettings["password"];
public List<MailInfo> LisMails
{
set { this._LisMails = value; }
get { return this._LisMails; }
}
public CallBack CB;
public ThreadData(CallBack cb)
{
this.CB = cb;
}
public void Run()
{
int i = 0;
foreach (MailInfo s in LisMails)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(string.Format(MailMessage,s.MailName+s.MailAddress));
CB(false,++i);
// SendMail20(s.MailAddress, s.MailName, string.Format(MailMessage, s.MailName));
}
CB(true, i);
}
private bool SendMail(string toMail, string subject, string body)
{
try
{
string fromMail = ThreadData.frommail;
MailMessage myMail = new MailMessage();
myMail.From = fromMail;
myMail.To = toMail;
//myMail.Cc = ccMail;
//myMail.Bcc = bccMail;
myMail.Subject = subject;
myMail.Body = body;
myMail.BodyFormat = MailFormat.Html;
//附件
string ServerFileName = "";
//if (this.upfile.PostedFile.ContentLength != 0)
//{
// string upFileName = this.upfile.PostedFile.FileName;
// string[] strTemp = upFileName.Split('.');
// string upFileExp = strTemp[strTemp.Length - 1].ToString();
// ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
// this.upfile.PostedFile.SaveAs(ServerFileName);
// myMail.Attachments.Add(new MailAttachment(ServerFileName));
//}
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", ThreadData.username); //发送方邮件帐户
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ThreadData.password); //发送方邮件密码
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
}
private bool SendMail20(string toMail, string subject, string body)
{
try
{
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(ThreadData.frommail); //邮件的发件人
//string fromMail = ThreadData.frommail;
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
//设置邮件的标题
myMail.Subject = subject;
//设置邮件的发件人
//Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用
myMail.From = from;
//MailAddress(address, displayName)
myMail.To.Add(new System.Net.Mail.MailAddress(toMail));
////设置邮件的抄送收件人
////这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好
//mail.CC.Add(new MailAddress("Manage@hotmail.com", "尊敬的领导");
//设置邮件的内容
myMail.Body = body;
//设置邮件的格式
myMail.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
myMail.IsBodyHtml = true;
//设置邮件的发送级别
myMail.Priority = System.Net.Mail.MailPriority.Normal;
//设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
//string fileName = txtUpFile.PostedFile.FileName.Trim();
//fileName = "D:\UpFile\" + fileName.Substring(fileName.LastIndexOf("\") + 1);
//txtUpFile.PostedFile.SaveAs(fileName); // 将文件保存至服务器
//mail.Attachments.Add(new Attachment(fileName));
myMail.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
//设置用于 SMTP 事务的主机的名称,填IP地址也可以了
client.Host = "smtp." + ThreadData.frommail.Substring(ThreadData.frommail.IndexOf("@") + 1);
//"smtp.hotmail.com";// "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
//设置用于 SMTP 事务的端口,默认的是 25
//client.Port = 25;
client.UseDefaultCredentials = false;
//这里才是真正的邮箱登陆名和密码,比如我的邮箱地址是 wangdetian@126.com, 我的用户名为 hbgx ,我的密码是 xgbh
client.Credentials = new System.Net.NetworkCredential(ThreadData.username, ThreadData.password);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//都定义完了,正式发送了,很是简单吧!
client.Send(myMail);
return true;
}
catch
{
return false;
}
}
}
public class MailInfo
{
public string MailName;
public string MailAddress;
public string MailMessage;
public MailInfo(string mailName, string mailAddress)
{
this.MailName = mailName;
this.MailAddress = mailAddress;
}
public MailInfo(string mailName, string mailAddress, string mailMessage)
{
this.MailName = mailName;
this.MailAddress = mailAddress;
this.MailMessage = mailMessage;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mail;
namespace ConsoleApplication1
{
public delegate void CallBack(bool state, int count);
public class Program
{
public void Show(bool state,int count)
{
Console.WriteLine("{0}-{1}",state,count);
}
static void Main(string[] args)
{
int tempCount = 120;
int tempWhileCount = (tempCount / 100) * 100;
int tempOtherCount = tempCount - tempWhileCount;
Console.WriteLine(tempWhileCount);
Console.WriteLine(tempOtherCount);
//指定线程数
int size = 10;
size = tempWhileCount==0?(0):(tempWhileCount <= 100 ? (10) : (tempWhileCount <= 1000 ? (20) : (25)));
List<MailInfo> lall = new List<MailInfo>();
//添加邮件信息
for (int i = 0; i <tempCount; i++)
{
lall.Add(new MailInfo("三毛"+ i.ToString(),"wangdetian16" + i.ToString() + "@qq.com"));
}
//创建多线程
for (int i = 0; i < size; i++)
{
ThreadData temptd = new ThreadData(new CallBack(new Program().Show));
List<MailInfo> tempLi = new List<MailInfo>();
for (int j = i * (tempWhileCount / size); j < (i * (tempWhileCount / size) + tempWhileCount / size); j++)
{
tempLi.Add(lall[j]);
}
temptd.LisMails = tempLi;
Thread tempThread = new Thread(new ThreadStart(temptd.Run));
tempThread.Start();
}
ThreadData temptdOther = new ThreadData(new CallBack(new Program().Show));
List<MailInfo> tempLiOther = new List<MailInfo>();
for (int j = tempWhileCount; j < tempCount; j++)
{
tempLiOther.Add(lall[j]);
}
temptdOther.LisMails = tempLiOther;
Thread tempThreadOther = new Thread(new ThreadStart(temptdOther.Run));
tempThreadOther.Start();
//ThreadData thdata1 = new ThreadData();
//List<string> l1 = new List<string>();
//for (int i = 0; i < 1000; i++)
//{
// l1.Add("wangdetian16"+i.ToString()+"@163.com");
//}
//thdata1.LisMails = l1;
//Thread thread = new Thread(new ThreadStart(thdata1.Run));
//thread.Start();
//ThreadData thdata2 = new ThreadData();
//List<string> l2 = new List<string>();
//for (int i = 0; i < 10; i++)
//{
// l2.Add("wangdetian18" + i.ToString() + "@163.com");
//}
//thdata2.LisMails = l2;
//Thread thread1 = new Thread(new ThreadStart(thdata2.Run));
//thread1.Start();
}
}
public class ThreadData
{
public static string MailMessage = "{0}你好你人物要做近三分萨拉夫接撒立法精神多了几分当上了副驾驶的发生大风蓝色队服那里公司发展规划的";
private List<MailInfo> _LisMails;
public static string frommail = "wangdetian116688@163.com";//System.Configuration.ConfigurationManager.AppSettings["frommail"];
public static string username = "wangdetian116688";//System.Configuration.ConfigurationManager.AppSettings["username"];
public static string password = "987654321";//System.Configuration.ConfigurationManager.AppSettings["password"];
public List<MailInfo> LisMails
{
set { this._LisMails = value; }
get { return this._LisMails; }
}
public CallBack CB;
public ThreadData(CallBack cb)
{
this.CB = cb;
}
public void Run()
{
int i = 0;
foreach (MailInfo s in LisMails)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(string.Format(MailMessage,s.MailName+s.MailAddress));
CB(false,++i);
// SendMail20(s.MailAddress, s.MailName, string.Format(MailMessage, s.MailName));
}
CB(true, i);
}
private bool SendMail(string toMail, string subject, string body)
{
try
{
string fromMail = ThreadData.frommail;
MailMessage myMail = new MailMessage();
myMail.From = fromMail;
myMail.To = toMail;
//myMail.Cc = ccMail;
//myMail.Bcc = bccMail;
myMail.Subject = subject;
myMail.Body = body;
myMail.BodyFormat = MailFormat.Html;
//附件
string ServerFileName = "";
//if (this.upfile.PostedFile.ContentLength != 0)
//{
// string upFileName = this.upfile.PostedFile.FileName;
// string[] strTemp = upFileName.Split('.');
// string upFileExp = strTemp[strTemp.Length - 1].ToString();
// ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
// this.upfile.PostedFile.SaveAs(ServerFileName);
// myMail.Attachments.Add(new MailAttachment(ServerFileName));
//}
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", ThreadData.username); //发送方邮件帐户
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ThreadData.password); //发送方邮件密码
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
}
private bool SendMail20(string toMail, string subject, string body)
{
try
{
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(ThreadData.frommail); //邮件的发件人
//string fromMail = ThreadData.frommail;
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
//设置邮件的标题
myMail.Subject = subject;
//设置邮件的发件人
//Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用
myMail.From = from;
//MailAddress(address, displayName)
myMail.To.Add(new System.Net.Mail.MailAddress(toMail));
////设置邮件的抄送收件人
////这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好
//mail.CC.Add(new MailAddress("Manage@hotmail.com", "尊敬的领导");
//设置邮件的内容
myMail.Body = body;
//设置邮件的格式
myMail.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
myMail.IsBodyHtml = true;
//设置邮件的发送级别
myMail.Priority = System.Net.Mail.MailPriority.Normal;
//设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
//string fileName = txtUpFile.PostedFile.FileName.Trim();
//fileName = "D:\UpFile\" + fileName.Substring(fileName.LastIndexOf("\") + 1);
//txtUpFile.PostedFile.SaveAs(fileName); // 将文件保存至服务器
//mail.Attachments.Add(new Attachment(fileName));
myMail.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
//设置用于 SMTP 事务的主机的名称,填IP地址也可以了
client.Host = "smtp." + ThreadData.frommail.Substring(ThreadData.frommail.IndexOf("@") + 1);
//"smtp.hotmail.com";// "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
//设置用于 SMTP 事务的端口,默认的是 25
//client.Port = 25;
client.UseDefaultCredentials = false;
//这里才是真正的邮箱登陆名和密码,比如我的邮箱地址是 wangdetian@126.com, 我的用户名为 hbgx ,我的密码是 xgbh
client.Credentials = new System.Net.NetworkCredential(ThreadData.username, ThreadData.password);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//都定义完了,正式发送了,很是简单吧!
client.Send(myMail);
return true;
}
catch
{
return false;
}
}
}
public class MailInfo
{
public string MailName;
public string MailAddress;
public string MailMessage;
public MailInfo(string mailName, string mailAddress)
{
this.MailName = mailName;
this.MailAddress = mailAddress;
}
public MailInfo(string mailName, string mailAddress, string mailMessage)
{
this.MailName = mailName;
this.MailAddress = mailAddress;
this.MailMessage = mailMessage;
}
}
}
发表评论
-
C# 去所有 html标签的代码|去除html标签代码
2010-07-23 15:12 365C# 去所有 html标签的代 ... -
vs2008下载|vs2008中文版下载
2010-08-20 17:56 265vs2008下载 中文版下载 官方下载地址 vs2008中文版 ... -
vs2005下载|中文版|官方
2010-08-20 18:20 281vs2005下载,中文版,官方 为了招徕更多的拥戴者,微软把V ... -
vs2010破解|vs2010破解方法|vs2010破解序列号
2010-08-27 13:29 254步骤1.下载VS2010 正式版,下载地址: 页面:h ... -
给xml|web.config 增加节点和属性值
2010-03-28 16:50 231给xml|web.config 增加节点和属性值 str ... -
C# 手动读写app config 的源码
2010-04-26 15:36 279public class ConfigOperator { ... -
asp.net md5的加密方法|md5加密方法asp.net
2010-07-06 22:16 282asp.net md5的加密方法|md5加密方法asp.net ... -
C# 上传生成新图片代码|C#生成图片代码
2010-07-07 15:01 522public void MakeSmallPic ... -
C# 去所有 html标签的代码|去除html标签代码
2010-07-23 15:12 237C# 去所有 html标签的代 ... -
c#多线程传值回调发邮件显示进行状态
2010-07-29 09:49 263<!--<br/ /><br/ /& ... -
vs2008下载|vs2008中文版下载
2010-08-20 17:56 263vs2008下载 中文版下载 官方下载地址 vs2008中文版 ... -
vs2005下载|中文版|官方
2010-08-20 18:20 614vs2005下载,中文版,官方 为了招徕更多的拥戴者,微软把V ... -
vs2010破解|vs2010破解方法|vs2010破解序列号
2010-08-27 13:29 333步骤1.下载VS2010 正式版,下载地址: 页 ...
相关推荐
本文将对C#窗体传值的几种主要方法进行详细的总结,包括属性传值、构造函数传值、事件委托传值、直接传值、回传以及多窗体之间的传值策略。 1. **属性传值**: 在C#中,窗体对象通常包含多个属性,这些属性可以...
委托允许我们传递方法作为参数,或者将方法作为其他方法的返回值,这为事件处理和回调机制提供了基础。 在这个实例中,我们创建了一个委托类型,例如`ValueTransferEventHandler`,它定义了方法签名,用于接收子...
9. **线程同步技术**:在多线程环境中,为了防止数据竞争,需要使用同步机制,如互斥锁`Mutex`、信号量`Semaphore`、临界区`CriticalSection`或监视器`Monitor`。 10. **异步编程与`async/await`**:C# 5.0引入的...
这种机制使得我们可以创建回调函数或者事件处理程序,使得代码更加灵活。在上述例子中,小明买书的过程被比喻为一个委托的使用:小明定义了一个委托`BuyBook`,然后将其与书店提供的方法`Book`绑定,最后通过调用`...
在实际应用中,你可能需要结合其他技术,比如文件I/O操作(例如读写配置文件或日志),网络通信(如HTTP请求),甚至多线程和异步操作,以实现更复杂的自动化任务。 附带的“小软件.png”可能是显示了运行上述代码...
- 对于大量图片上传,考虑使用多线程或异步处理以提高性能。 通过以上步骤,我们可以实现C# Ajax调用WebAPI上传图片并传值的功能,同时结合图片相似度识别对比,为用户提供高效、流畅的交互体验。在实际开发中,还...
而逆向传值则通常是通过某种机制,使得被调用者能够将值传递回调用者,或者在调用链中的某个中间环节改变调用者的状态,这种机制往往涉及到全局变量、静态成员、回调函数或者事件驱动等。 单例模式是一种创建型设计...
这使得我们可以实现回调机制,事件处理和策略模式等高级编程技术。 2. **Action委托的定义** `Action`是.NET Framework提供的一个预定义委托类型,它表示没有返回值但可以有任意数量参数的方法。例如,`Action, ...
委托是.NET中的一个强大特性,它允许我们将方法作为参数传递,从而实现函数回调或者事件处理。 首先,了解委托的基本概念。委托在C#中类似于C++的函数指针,但更安全,支持类型检查和多播。委托类型定义了一种调用...
我们可以创建自定义的委托类型,这通常与事件处理或回调机制有关。在我们的例子中,我们将创建一个委托类型来传递数据和执行操作。 ```csharp public delegate void ValueTransferEventHandler(object sender, ...
C#中的委托可以看作是函数指针的高级形式,用于实现事件和回调。 #### 13. Override与Overload的区别? - **Override**:用于子类覆盖父类的方法,要求方法签名完全相同。 - **Overload**:在同一类中定义多个同名...
通过使用委托,我们可以创建事件处理程序、实现回调机制,甚至进行异步操作。`System.Action`和`System.Func`是C#内置的两种常用委托类型,分别用于无返回值和有返回值的方法。 在异步编程中,委托扮演着关键角色。...
Lambda表达式是C#中定义委托实例的简洁方式,它可以简化代码,特别是在处理事件或定义简单的回调时。 综上所述,委托是C#中非常重要的一个特性,它使得代码更加灵活,能够实现复杂的编程模式。理解并熟练运用委托...
- 配置ECharts事件监听器,当用户交互时,触发回调函数,读取并处理返回的数据。 8. **注意事项** - 确保`WebBrowser`控件的`IsScriptEnabled`属性设为`true`,以便执行JavaScript代码。 - 由于异步性质,可能...
目录 第一部分C#语言基础 第1章C#类型基础 1.1值类型和引用类型 1.1.1值类型 1.1.2引用类型 1.1.3简单类型 1.1.4装箱和拆箱 1.2对象判等 1.2.1 引用类型判等 ...第16章 多线程 第17章 对象生存期与垃圾收集