111,129
社区成员
发帖
与我相关
我的任务
分享 delegate bool sendMsgDelegate(string server, string username, string password, string toaddr, string titel, string body); //申明委托
private static object obj = new object();
static int count = 0; //计数
static int sucessCount = 0; //成功的数量
static int total = 0; //
private static AutoResetEvent autoEvent = new AutoResetEvent(false);
public static bool sendMsg(string server, string username, string password, string toaddr, string titel, string body)
{
return EmailBuild.SendMail(server, username, password, toaddr, titel, body);
// return true;
}
public static void SendCallBack(IAsyncResult asynceresult)
{
sendMsgDelegate ss = (sendMsgDelegate)asynceresult.AsyncState;
lock (obj)
{
count++;
bool result = ss.EndInvoke(asynceresult);
if (result)
{
sucessCount++;
}
if (count == total)
{
autoEvent.Set();
}
}
}
/// <summary>
/// 邮件群发
/// </summary>
/// <param name="title">主题</param>
/// <param name="body">内容</param>
/// <param name="list">会员集合</param>
public static void Send(string title, string body, IList<Patti.Model.CustomersInfo> list)
{
int n = 0;
IList<Patti.Model.MailsettingInfo> EmList = (new Patti.BLL.MailsettingService()).GetListByDataTable("*", "", "MailID", 1, 1, "0", out n);
if (EmList.Count <= 0)
{
Loggor.Log("邮箱发送", "GetCountAndTime", "邮箱参数不对");
}
string server = EmList[0].MailSmtp;
string username = EmList[0].MailName;
string password = EmList[0].MailPwd;
total = list.Count;
DateTime t1 = DateTime.Now;
sendMsgDelegate ss = new sendMsgDelegate(sendMsg);
for (int i = 0; i < list.Count; i++)
{
if (list[i].CEmail != "")
{
IAsyncResult asyceresult = ss.BeginInvoke(server, username, password, list[i].CEmail, title, body, SendCallBack, ss);
}
}
autoEvent.WaitOne();
DateTime t2 = DateTime.Now;
//watch.Stop();
Loggor.Log("邮箱发送", "GetCountAndTime", "会员总数为:" + list.Count + ";成功发送" + sucessCount + "个邮箱;总花费的时间为:" + (t2 - t1));
}
