public static Object search(Object o) { try { Hashtable param = (Hashtable)o; EngineEntity engine = (EngineEntity)param["engine"]; engine.Status = "处理中"; String searchKey = HttpUtility.UrlEncode((String)param["searchKey"], Encoding.UTF8); IEngine search = engine.Search; Dictionary <String, String> urlMap = new Dictionary <String, String>(); Int32 timeout = Convert.ToInt32(ConfigService.get("parse.request.timeout")) * 1000; String html = String.Empty; Int32 unParseURL = 0; //出错50次算结束,连续5次没抓到邮箱算结束,大于设置最大邮箱数算结束 while (!search.isEndPage(html, engine) && engine.PageCnt <= engine.MaxPageCnt && engine.ErrorCnt < 50 && unParseURL < 5) { String url = search.getPageUrl(html, engine, searchKey); try { html = WebClientUtils.getHTML(url, timeout); if (!String.Empty.Equals(html)) { if (search.addUrl(CommonUtils.getHTMLinkArray(url, html), engine) == 0) { unParseURL++; } else { unParseURL = 0; } } engine.PageCnt = engine.PageCnt + 1; } catch (Exception ex) { engine.ErrorCnt = engine.ErrorCnt + 1; search.error(engine, ex); html = String.Empty; } } engine.Status = "处理结束"; } catch { } return(null); }
private MailMessage getMailMessage(String toAddress, String fromAddress, String subject, String body, List <String> attachmentList) { MailMessage mail = new MailMessage(); if (fromAddress == null || "".Equals(fromAddress)) { fromAddress = ConfigService.get("mail.msg.from"); } mail.From = new MailAddress(fromAddress, ConfigService.get("mail.msg.fromName")); mail.ReplyTo = new MailAddress(ConfigService.get("mail.msg.replyTo"), ConfigService.get("mail.msg.replyToName")); if (toAddress != null && !"".Equals(toAddress)) { mail.To.Add(new MailAddress(toAddress)); } //设置邮件的标题 mail.Subject = subject; mail.SubjectEncoding = System.Text.Encoding.UTF8; //设置邮件的内容 mail.Body = body; mail.BodyEncoding = System.Text.Encoding.UTF8; //附件 if (attachmentList != null) { foreach (String path in attachmentList) { mail.Attachments.Add(new Attachment(path)); } } //设置邮件的抄送收件人 //mail.CC.Add(new MailAddress("*****@*****.**", "xxx")); //mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; //设置邮件的格式 mail.IsBodyHtml = Convert.ToBoolean(ConfigService.get("mail.send.userHTML")); //设置邮件的发送级别 mail.Priority = MailPriority.Normal; return(mail); }
public String testMail(String host, Int32 port, String username, String password, Boolean enableSsl, String mailAddress) { String result = null; try { SmtpClient client = getSmtpClient(host, port, username, password, enableSsl); MailMessage mail = getMailMessage(ConfigService.get("mail.send.testToAddress"), mailAddress, "测试", "测试内容", null); client.Send(mail); result = "测试连接成功"; } catch (Exception ex) { result = ex.Message; } return(result); }
private SmtpClient getSmtpClient(String host, Int32 port, String username, String password, Boolean enableSsl) { SmtpClient client = new SmtpClient(); client.Host = host; client.Port = port; client.EnableSsl = enableSsl; //client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Timeout = Convert.ToInt32(ConfigService.get("mail.send.timeout")) * 1000; if (!"".Equals(username)) { client.Credentials = new System.Net.NetworkCredential(username, password); } return(client); }
public void send(List <String[]> addressList, String title, String content, List <String> attachmentList) { sendWaitCnt = addressList.Count; sendSuccessCnt = 0; sendErrorCnt = 0; MailMessage mail = getMailMessage(null, null, title, content, attachmentList); Int32 maxResendCnt = Convert.ToInt32(ConfigService.get("mail.resendCnt")); for (Int32 i = 0; i < addressList.Count; i++) { String address = addressList[i][0]; try { mail.To.Clear(); mail.To.Add(new MailAddress(address)); } catch (Exception ex) { sendErrorCnt++; sendWaitCnt--; sendLog.AddFirst(DateUtils.getSysDate() + " 初始化发送邮件路径:" + address + "时出错:" + ex.Message); continue; } if (addressList.Count > 50 && i == Math.Ceiling(new Decimal(addressList.Count) / 3)) { recheckReg(addressList, content, attachmentList); } Int32 resendCnt = 0; MailClientEntity sender = null; while (resendCnt++ < maxResendCnt) { try { sender = getClient(); //设置form String fromAddress = !"".Equals(sender.MailAddress) ? sender.MailAddress : ConfigService.get("mail.msg.from"); mail.From = new MailAddress(fromAddress, ConfigService.get("mail.msg.fromName")); DateTime startTime = DateTime.Now; sender.SmtpClient.Send(mail); DateTime endTime = DateTime.Now; List <SQLiteParameter> paramList = new List <SQLiteParameter>(); paramList.Add(new SQLiteParameter("@mail_address", address)); paramList.Add(new SQLiteParameter("@send_date", DateUtils.getSysDate())); SQLiteUtils.execute("update t_mail_address set send_cnt = send_cnt+1, send_date = @send_date where mail_address = @mail_address", paramList); sendLog.AddFirst(DateUtils.getSysDate() + " 邮件:" + address + "发送成功,耗时:" + Convert.ToString(endTime.Subtract(startTime).TotalSeconds) + "秒,主机:" + (sender != null ? sender.Host : "") + ",用户名:" + (sender != null ? sender.UserName : "")); sender.ErrorCnt = 0; sendSuccessCnt++; sendWaitCnt--; break; } catch (Exception ex) { if (maxResendCnt == resendCnt + 1) { sendErrorCnt++; sendWaitCnt--; } //连续发送3次,停止使用此邮箱固定时间 if (sender.ErrorCnt++ >= 3) { sender.WaitEndTime = DateTime.Now.AddSeconds(Convert.ToDouble(ConfigService.get("mail.send.senderWaitTime"))); } sendLog.AddFirst(DateUtils.getSysDate() + " 邮件:" + address + "发送出错,主机:" + (sender != null ? sender.Host : "") + ",用户名:" + (sender != null ? sender.UserName : "") + ",异常:" + ex.Message); List <SQLiteParameter> paramList = new List <SQLiteParameter>(); paramList.Add(new SQLiteParameter("@id", sender.Id)); paramList.Add(new SQLiteParameter("@last_error_date", DateUtils.getSysDate() + ex.Message)); SQLiteUtils.execute("update t_mail_sender set last_error_date = @last_error_date where id = @id", paramList); } } while (sendLog.Count > 100) { sendLog.RemoveLast(); } } }
public void add(String url, String type) { url = url.Replace("\r\n", "").Replace(Environment.NewLine, "").Trim(); RuntimeEntity runtime = runtimeService.get(url); if (runtime == null) { runtime = runtimeService.add(url); } if (Constant.RunStatus.RUNING.Equals(runtime.RunStatus)) { try { if (get(url) == null) { runtime.WaitQueue.Add(url, new ParseEntity(url, type)); Hashtable param = new Hashtable(); param.Add("url", url); param.Add("type", type); lock (runtime.WorkItemsGroup) { ThreadPoolEx.add(runtime.WorkItemsGroup, new WorkItemCallback(ParseThread.run), param, runtime.Priority); } } } catch { } //等待解析数超过系统设定,则优先级降低 if (!WorkItemPriority.Lowest.Equals(runtime.Priority) && (runtime.WaitQueue.Count + runtime.StartQueue.Count + runtime.SuccessQueue.Count) > Convert.ToInt32(ConfigService.get("parse.lowParseCnt"))) { runtime.Priority = WorkItemPriority.Lowest; } //已解析数超过系统设定,则停止 if (runtime.WaitQueue.Count + runtime.StartQueue.Count + runtime.SuccessQueue.Count + runtime.ErrorQueue.Count >= Convert.ToInt32(ConfigService.get("parse.rule.maxParseCnt"))) { runtime.RunStatus = Constant.RunStatus.STOPING; } } }