private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtSmtpServer.Text)) { MessageBox.Show("SMTP服务器不能为空", "系统提示"); return; } if (string.IsNullOrEmpty(this.txtSmtpPort.Text)) { MessageBox.Show("SMTP服务器端口不能为空", "系统提示"); return; } if (string.IsNullOrEmpty(this.txtUserName.Text)) { MessageBox.Show("登录用户名不能为空", "系统提示"); return; } SmtpList info = new SmtpList(); info.SmtpServer = this.txtSmtpServer.Text; info.SmtpPort = this.txtSmtpPort.Text.ToInt(25); info.UserName = this.txtUserName.Text; info.SPassword = this.txtSPassword.Text; info.SSL = this.checkBoxSSL.Checked; info.Status = this.checkBoxStatus.Checked == true ? 0 : 1; //bool isTrue = NetHelper.CheckSMTP(info.UserName, info.SPassword, info.SmtpServer); //if (!isTrue) { // MessageBox.Show("无法连接SMTP服务器,请检查用户名和密码是否正确和账号是否被封。", " 系统提示"); // return; //} DialogResult = System.Windows.Forms.DialogResult.OK; if (!smtp.IsNullEmpty() || SmtpListHelper.IsExistByID(info.SmtpServer, info.SmtpPort.Value, info.UserName)) { SmtpListHelper.Update(info); } else { info.CreateTime = DateTime.Now.ToDateTime().ToDateTime(); SmtpListHelper.Insert(info); } SmtpListHelper.ClearCacheAll(); MessageBox.Show("保存数据成功!", " 系统提示"); this.Close(); }
private void mnuEnable_Click(object sender, EventArgs e) { switch (type) { case 1: EmailListHelper.ClearCacheAll(); break; case 2: SmtpListHelper.ClearCacheAll(); foreach (ListViewItem item in listView1.Items) { if (item.Checked) { string[] list = item.Tag.ToString().Split(','); SmtpList info = new SmtpList(); info.SmtpServer = list[0].ToString(); info.SmtpPort = list[1].ToString().ToInt(25); info.UserName = list[2].ToString(); info.SPassword = list[3].ToString(); info.Status = 0; SmtpListHelper.Update(info); //if (!info.SmtpServer.IsNullEmpty() || SmtpListHelper.IsExistByID(info.SmtpServer, info.SmtpPort.Value, info.UserName)) { // SmtpListHelper.Update(info); //} } } break; case 3: IpHistoryHelper.ClearCacheAll(); break; } BindData(); }
/// <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(); //清理数据 }
private void inportSmtpList(object o) { int totals = 0; int success = 0; WriteLog(""); WriteLog("正在读取数据....."); IList<string> data = FileDirectory.FileRead(fileName, FileDirectory.FileEncoding(fileName)); totals = data.Count; WriteLog("读取到:" + totals + " 行记录!"); WriteLog("正在导入数据....."); int index = 1; int port = 25; foreach (string str in data) { string[] list = str.Split(','); if (list.Length != 3) { WriteLog("第" + index.ToString() + "行数据格式不正确:" + str); } else { string smtp = list[0].Trim(); string userName = list[1].Trim(); if (!IsSmtp(smtp)) { WriteLog("第" + index.ToString() + "行Smtp服务器地址格式不正确:" + str); } else { if (SmtpListHelper.IsExistByID(smtp, port, userName)) { WriteLog("第" + index.ToString() + "行数据已存在:" + str); } else { //bool isTrue = NetHelper.CheckSMTP(list[1], list[2], list[0]); //if (isTrue) { SmtpList info = new SmtpList(); info.SmtpServer = list[0]; info.UserName = list[1]; info.SPassword = list[2]; info.SmtpPort = 25; info.CreateTime = DateTime.Now.ToDateTime().ToDateTime(); SmtpListHelper.Insert(info); success++; //} else { // WriteLog("第" + index.ToString() + "行SMTP无法连接到服务器,请检查用户名和密码是否正确和账号是否被封:" + str); //} } } } index++; } SmtpListHelper.ClearCacheAll(); WriteLog("导入Smtp服务器列表完成:共 {0} 条记录,成功 {1} 条记录,失败 {2} 条记录!".FormatWith(totals, success, totals - success)); }
private int SendEmails(int sends = 0) { int sendIndex = 0; //当前发送的邮件索引 int ips = 0; //记录发送多少邮件后更换IP int smtps = 0; int smtps2 = 0; int smtpIndex = 0; //记录发送多少邮件后更换SMTP、smtp发送失败次数、当前使用的smtp索引 EmailListHelper.ClearCacheAll(); Where where = new Where().And("LastSendStatus", 0, Operator.Equal); emailList = EmailListHelper.SelectListByAll(null, where).Where(e => e.LastSendError.IndexOf("Mailbox not found") == -1).ToList(); if (emailList.Count == 0) return 0; //0正常发送结束 //循环发送所有email foreach (EmailList e in emailList) { sendIndex++; if (exit) { clear(); WriteLog("已停止发送邮件!"); thread.Abort(); return -1; } //-1停止发送邮件 string name = smtpInfo.UserName; if (name.IndexOf("@") != -1) name = name.Split('@')[0]; //显示发件人 int index = smtpInfo.SmtpServer.IndexOf("."); string smtpDomail = smtpInfo.SmtpServer.Substring(index + 1); string emailAddress = name + "@" + smtpDomail; //显示发件人EMAIL if (!template.ShowName.IsNullEmpty()) name = template.ShowName; //显示模版里设置的发件人 bool success = false; string msg = string.Empty; if (emailAddress.Trim().ToLower() == e.EmailAddress.Trim().ToLower()) success = true; //不允许自己发给自己 if (!success) { success = email.ClearTo() .From(name, emailAddress) .Subject(processText(template.Subject, e)) .Body(processText(template.Body, e)) .To(t => t.Add(e.NickName.IfNullOrEmpty(e.EmailAddress.Split('@')[0]), e.EmailAddress)) .Send(); msg = processError(email.ErrorMessage); if (sendSetting.SendInterval > 0) Thread.Sleep(sendSetting.SendInterval.Value); //暂停时间 } WriteLog("<" + e.NickName + ">" + e.EmailAddress + (success ? " 发送成功!" : " 发送出错:" + msg)); //最后一次发送情况 EmailList emailInfo = new EmailList(); emailInfo.EmailAddress = e.EmailAddress; emailInfo.LastSendStatus = success ? 1 : (sendSetting.SendRetryCount == e.SendCount + 1 ? 2 : 0); emailInfo.LastSendTime = DateTime.Now; emailInfo.LastSendSmtp = smtpInfo.SmtpServer + "|" + smtpInfo.UserName; emailInfo.LastSendError = msg; emailInfo.SendCount = e.SendCount + 1; EmailListHelper.Update(emailInfo); ips++; if (sendSetting.IPInterval > 0 && ips >= sendSetting.IPInterval) { //换IP ips = 0; WriteLog("正在更换IP....."); string ip = NetHelper.ChangeIP(sendSetting.ConnectType.Value, uiMsg, uiDone); if (ip.IsNullEmpty()) { WriteLog(sendSetting.MaxRetryCount + "次更换IP失败!"); return 1; } //1更换IP失败结束 WriteLog("更换IP:" + ip); } smtps++; if (!success) smtps2++; if (sendSetting.SmtpInterval > 0 && smtps >= sendSetting.SmtpInterval && smtpList.Count > 1) { //换SMTP UpdateSmtpListSendCount(smtpIndex, sendSetting.SmtpInterval.Value, smtps2); //更新smtp发送次数 smtps = 0; smtps2 = 0; smtpIndex++; if (smtpIndex + 1 > smtpList.Count) smtpIndex = 0; //如果所有smtp发送完,将重新循环更换 smtpInfo = smtpList[smtpIndex]; //更换下一个smtp WriteLog("更换SMTP:" + smtpInfo.SmtpServer + "|" + smtpInfo.UserName); email.Host(smtpInfo.SmtpServer) .Ssl(smtpInfo.SSL.Value) .Port(smtpInfo.SmtpPort.Value) .Credentials(smtpInfo.UserName, smtpInfo.SPassword); } }; if (smtps > 0) UpdateSmtpListSendCount(smtpIndex, smtps, smtps2); //更新smtp发送次数 WriteLog("第" + (sends + 1) + "轮发送完成!"); return SendEmails(sends + 1); }
/// <summary> /// �ļ�¼ /// </summary> /// <param name="smtpList">ʵ����</param> /// <returns>���Ƿ�ɹ�</returns> public static bool Update(SmtpList smtpList, string dbkey) { return Update(smtpList, dbkey, null, null); }
/// <summary> /// �ļ�¼ /// </summary> /// <param name="smtpList">ʵ����</param> /// <param name="where">��ʱ����������ͳһ��ǰ��Ҫ�����ӷ���and��or�ȵȣ�</param> /// <param name="delCache">�ijɹ��������CACHE key��֧������</param> /// <param name="dbkey">�������ݿ����ӳ��е�����key��Ϊ��ʱʹ��ConnString����</param> /// <returns>���Ƿ�ɹ�</returns> public static bool Update(SmtpList smtpList, string dbkey = "", Where where = null, string[] delCache = null) { if (smtpList.SmtpServer.IsNullEmpty() && smtpList.SmtpPort.IsNull() && smtpList.UserName.IsNullEmpty()) return false; int value = new SQL().Database(dbkey).Update(SmtpList._) .SetP(SmtpList._SPassword, smtpList.SPassword) .SetP(SmtpList._SSL, smtpList.SSL) .SetP(SmtpList._Status, smtpList.Status) .SetP(SmtpList._Sends, smtpList.Sends) .SetP(SmtpList._SendFails, smtpList.SendFails) .SetP(SmtpList._CreateTime, smtpList.CreateTime) .Where(new Where() .AndP(SmtpList._SmtpServer, smtpList.SmtpServer, Operator.Equal, true) .AndP(SmtpList._SmtpPort, smtpList.SmtpPort, Operator.Equal, true) .AndP(SmtpList._UserName, smtpList.UserName, Operator.Equal, true) ).Where(where).ToExec(); if (value <= 0) return false; if (delCache.IsNull()) return true; Cache2.Remove("TH.Mailer.SmtpListCache_", delCache); return true; }
/// <summary> /// ��Ӽ�¼ /// </summary> /// <param name="smtpList">ʵ����</param> /// <returns>����Ƿ�ɹ�</returns> public static bool Insert(SmtpList smtpList, string dbkey) { return Insert(smtpList, dbkey, null); }
/// <summary> /// ��Ӽ�¼ /// </summary> /// <param name="smtpList">ʵ����</param> /// <param name="delCache">��ӳɹ��������CACHE key��֧������</param> /// <param name="dbkey">�������ݿ����ӳ��е�����key��Ϊ��ʱʹ��ConnString����</param> /// <returns>����Ƿ�ɹ�</returns> public static bool Insert(SmtpList smtpList, string dbkey = "", string[] delCache = null) { int obj = new SQL().Database(dbkey).Insert(SmtpList._) .ValueP(SmtpList._SmtpServer, smtpList.SmtpServer) .ValueP(SmtpList._SmtpPort, smtpList.SmtpPort) .ValueP(SmtpList._UserName, smtpList.UserName) .ValueP(SmtpList._SPassword, smtpList.SPassword) .ValueP(SmtpList._SSL, smtpList.SSL) .ValueP(SmtpList._Status, smtpList.Status) .ValueP(SmtpList._Sends, smtpList.Sends) .ValueP(SmtpList._SendFails, smtpList.SendFails) .ValueP(SmtpList._CreateTime, smtpList.CreateTime) .ToExec(); if (delCache.IsNull()) return obj == 1; Cache2.Remove("TH.Mailer.SmtpListCache_", delCache); return obj == 1; }