private void button2_Click(object sender, EventArgs e) { string text = ((Button)sender).Text; string code = "SmtpClient"; if (text.StartsWith("SmtpClient")) code = "SmtpClient"; if (text.StartsWith("SmtpMail")) code = "SmtpMail"; if (text.StartsWith("CDO.Message")) code = "CDOMessage"; if (text.StartsWith("TcpClient")) code = "TcpClient"; if (text.StartsWith("Blat")) code = "Blat"; ((Button)sender).Enabled = false; new Thread(() => { var status = new Email("Pub.Class.Email.{0}.SendEmail,Pub.Class.Email.{0}".FormatWith(code)) .Server(server2.Host, server2.Port) .From(server2.From) .Body(body.FormatWith(text)) .Subject(subject.FormatWith(text)) .IsBodyHtml(false) .UseDefaultCredentials(true) .To(to => to.Add(server2.To)) .Send(); MessageBox.Show(subject.FormatWith(text) + "发送{0}!".FormatWith(status ? "成功" : "失败")); ((Button)sender).Enabled = true; }).Start(); }
/// <summary> /// 开始群发邮件 /// </summary> private void sendStart() { UpdateSendSettingStatus(1); //开始发送并初始化数据 smtpList = SmtpListHelper.SelectListByAll().Where(p => p.Status == 0).ToList(); if (smtpList.Count == 0) { WriteLog("SMTP列表为空!"); if (uiDone != null) uiDone(); return; } smtpInfo = smtpList[0]; //默认使用第一个SMTP发送 template = templateList.Where(t => t.TemplateID == sendSetting.TemplateID).FirstOrDefault(); if (template.IsNull() || template.TemplateID.IsNull()) { WriteLog("找不到模版ID:" + sendSetting.TemplateID); if (uiDone != null) uiDone(); return; } WriteLog(""); WriteLog(template.Subject + "|" + NetHelper.GetNetName(sendSetting.ConnectType.Value) + "|" + smtpInfo.SmtpServer + "|" + smtpInfo.UserName + " 开始发送!"); email = new Email(smtpInfo.SmtpServer, smtpInfo.SmtpPort.Value) .Ssl(smtpInfo.SSL.Value) .Credentials(smtpInfo.UserName, smtpInfo.SPassword) .IsBodyHtml(template.IsHTML.Value) .Timeout(3000); int state = SendEmails(); if (state == -1) return; //停止发送邮件 if (state == 0) UpdateSendSettingStatus(2); //正常发送完成时 标记全部发送完成 WriteLog(template.Subject + (state == 0 ? " 已发送完成!" : " 已停止发送!")); //此处可邮件通知 if (uiDone != null) uiDone(); clear(); //清理数据 }