private void DeleteMail(DateTime datetime) { using (var pop3 = new POP3_Client()) { pop3.Connect(pop3host, 995, true); pop3.Timeout = 3000; pop3.Login(user, pwd); var date = datetime.ToString("yyyy-MM-dd"); var del = 0; foreach (POP3_ClientMessage m in pop3.Messages) { var header = Mail_Message.ParseFromByte(m.HeaderToByte()); var ss = header.Subject.Split('@'); if (ss.Length != 2) { m.MarkForDeletion(); continue; } if (ss[0] == this.shop && ss[1] == date) { m.MarkForDeletion(); sb.Append("delete old mail: " + date + "\r\n"); del++; continue; } } sb.Append(string.Format("共{0}邮件,删除旧邮件{1}\r\n", pop3.Messages.Count, del)); pop3.Disconnect(); } }
public override Server creack(String ip, int port, String username, String password, int timeOut) { POP3_Client conn = null; Server server = new Server(); try { conn = new POP3_Client(); //与Pop3服务器建立连接 conn.Timeout = timeOut; conn.Connect(ip, port, true); if (conn.IsConnected) { conn.Login(username, password); if (conn.IsAuthenticated) { server.isSuccess = conn.IsAuthenticated; server.banner = conn.GreetingText; } } } catch (Exception e) { throw e; } finally { if (conn != null && conn.IsConnected) { conn.Disconnect(); } } return(server); }
private void UpdateData() { using (var pop3 = new POP3_Client()) { pop3.Connect(this.pop3Host, 995, true); pop3.Login(this.user, this.password); int delete = 0; int insert = 0; int old = 0; int sum = pop3.Messages.Count; int newShop = 0; foreach (POP3_ClientMessage m in pop3.Messages) { var header = Mail_Message.ParseFromByte(m.HeaderToByte()); var subject = header.Subject; var ss = subject.Split('@'); // "wkl@2017-01-01" if (ss.Length != 2) { m.MarkForDeletion(); delete++; continue; } if (!shops.Keys.Contains <string>(ss[0])) { newShop++; continue; } DateTime dt; if (!DateTime.TryParse(ss[1], out dt)) { m.MarkForDeletion(); delete++; continue; } if (this.IsOldUid(m.UID)) //如果已读取过的邮件跳过 { if (this.IsOldDate(dt)) { m.MarkForDeletion(); //过期删除 delete++; } old++; continue; } var mail = Mail_Message.ParseFromByte(m.MessageToByte()); var xml = new XmlDocument(); xml.LoadXml(mail.BodyText); this.InsertData(xml, dt); this.InsertOldMail(m.UID, dt); insert++; } MessageBox.Show(string.Format("共 {0} 条邮件,删除过期邮件 {1}, 略过已读邮件 {2}, 读取新邮件 {3}.\r\n未识别门店邮件 {4}, (如此值大于零, 请添加新门店!)", sum, delete, old, insert, newShop), "数据处理报告:", MessageBoxButtons.OK, MessageBoxIcon.Information); pop3.Disconnect(); } }
static void Main() { var user = "******"; var pwd = "xAcmQ3gg"; const string pop3Server = "smtp.united-imaging.com"; const int pop3Port = 995; // Connect Elasticsearch var node = new Uri("http://10.6.14.157:9200"); var elasticUser = "******"; var elasticPwd = "123qwe"; var setting = new ConnectionSettings(node); setting.BasicAuthentication(elasticUser, elasticPwd); var elasticSearchClient = new ElasticClient(setting); // Index Mapping var descriptor = new CreateIndexDescriptor("mail") .Mappings(ms => ms.Map <Mail>(m => m.AutoMap())); elasticSearchClient.CreateIndex(descriptor); // Scheduled Mail Collecting while (true) { using (var pop3 = new POP3_Client()) { pop3.Connect(pop3Server, pop3Port, true); pop3.Login(user, pwd); var messages = pop3.Messages; var messagesCount = messages.Count; Console.WriteLine("Message Count: {0}", messagesCount); for (int i = 0; i < messagesCount; i++) { Console.WriteLine("Indexing Mail: {0} of {1}", i, messagesCount); var message = messages[i]; var mail = IndexMessage(message); CheckMailToToSupervisor(mail); var respose = elasticSearchClient.Index(mail, idx => idx.Index("mail")); Console.WriteLine("Indexed a mail with respose : {0}", respose.Result.ToString()); Console.WriteLine(); message.MarkForDeletion(); } pop3.Disconnect(); } Thread.Sleep(5 * 60 * 1000); } //Console.WriteLine("Press any key to continue"); //Console.ReadKey(); }
public override void Close() { if (client != null && client.IsConnected) { client.Disconnect(); client.Dispose(); } channelState = ChannelState.Closed; }
public static List <Email> GetAllEmails(string server, int port, string user, string pwd, bool usessl, bool deleteafterread, ref List <string> errors) { var ret = new List <Email>(); errors.Clear(); var oClient = new POP3_Client(); try { oClient.Connect(server, port, usessl); oClient.Authenticate(user, pwd, true); } catch (Exception exception) { errors.Add("Error connect/authenticate - " + exception.Message); return(null); } foreach (POP3_ClientMessage message in oClient.Messages) { var wrapper = new Email(); wrapper.Uid = message.UID; try { Mail_Message mime = Mail_Message.ParseFromByte(message.HeaderToByte()); wrapper.Subject = mime.Subject; wrapper.From = mime.From[0].Address; wrapper.To = mime.To[0].ToString(); mime = Mail_Message.ParseFromByte(message.MessageToByte()); string sa = mime.BodyHtmlText; if (sa == null) { wrapper.TextBody = mime.BodyText; } else { wrapper.HtmlBody = sa; } } catch (Exception exception) { errors.Add("Error reading " + wrapper.Uid + " - " + exception.Message); } if (deleteafterread) { message.MarkForDeletion(); } ret.Add(wrapper); } oClient.Disconnect(); oClient.Dispose(); return(ret); }
public List <Mail_Message> ReciveEmail() { List <Mail_Message> mailMsgs = new List <Mail_Message>(); POP3_Client pop3 = new POP3_Client(); //pop3.Connect("pop3.sina.com.cn",110); pop3.Connect(Pop3Server, Pop3Port, SSL); //pop3.Login("lulufaer", "ljc123456"); pop3.Login(MailAccount, MailPassword); if (pop3.IsAuthenticated) { for (int i = 0; i < pop3.Messages.Count; i++) { //MailMsg msg= new MailMsg(); byte[] bytes = pop3.Messages[i].MessageToByte(); Mail_Message m_msg = Mail_Message.ParseFromByte(bytes); //msg.msgFrom = m_msg.From[0].Address; //msg.msgTime = m_msg.Date; //msg.msgTo = m_msg.To.Mailboxes[0].Address; //msg.MsgSubject = m_msg.Subject; //msg.MsgContent = m_msg.BodyText; mailMsgs.Add(m_msg); if (DelAfterRecived) { pop3.Messages[i].MarkForDeletion(); //pop3.Messages[i].MarkForDeletion(); //pop3.Messages[i].MarkForDeletion(); } } } pop3.Disconnect(); return(mailMsgs); }
private void LumiSoftMail() { string sendmail, HtmlBody = "", date; //网络连接判断 if (!IsNetConnect()) { return; } POP3_Client popMail = new POP3_Client(); try { popMail.Timeout = HtmlTextPath.EMAIL_TIME_OUT; if (string.IsNullOrEmpty(MAIL_USER_NAME) || string.IsNullOrEmpty(MAIL_USER_PWD) || string.IsNullOrEmpty(MAIL_POP) || string.IsNullOrEmpty(MAIL_SENDER) || string.IsNullOrEmpty(PRT_COUNT) || string.IsNullOrEmpty(COMPANY_NAME)) { QueryUser(); } try { popMail.Connect(MAIL_POP, HtmlTextPath.EMAIL_PORT, false); } catch (Exception ex) { Console.Out.WriteLine("ex:" + ex); SetRichTextValue(DateTime.Now.ToString("o") + @"######Internet connection failed######"); return; //throw; } //登录邮箱地址 popMail.Login(MAIL_USER_NAME, MAIL_USER_PWD); } catch (Exception ex) { Console.Out.WriteLine("ex:" + ex); //richTextBox1.Text += System.Environment.NewLine + "ex:" + ex; //Console.Out.WriteLine("Can not connect email server" + DateTime.Now.ToString("o")); //richTextBox1.Text += System.Environment.NewLine + "Can not connect email server:" + DateTime.Now.ToString("o"); //SetRichTextValue("MAIL_USER_NAME:" + MAIL_USER_NAME + "MAIL_USER_PWD:" + MAIL_USER_PWD + "MAIL_POP:" + MAIL_POP + "MAIL_SENDER:" + MAIL_SENDER + "PRT_COUNT:" + PRT_COUNT + "COMPANY_NAME:" + COMPANY_NAME); SetRichTextValue(DateTime.Now.ToString("o") + @"######Can not connect email server######"); return; } Console.Out.WriteLine(DateTime.Now.ToString("o")); SetRichTextValue(DateTime.Now.ToString("o")); POP3_ClientMessageCollection messagesCollection = popMail.Messages; POP3_ClientMessage message = null; //存放需删除的邮件 List <POP3_ClientMessage> lstMessage = new List <POP3_ClientMessage>(); string strMessage = ""; if (0 < messagesCollection.Count) { //for (int i = messagesCollection.Count - 1; i >= 0; i--) for (var index = 0; index < popMail.Messages.Count; index++) { POP3_ClientMessage mail = popMail.Messages[index]; try { message = mail; } catch (Exception) { popMail.Timeout = HtmlTextPath.EMAIL_TIME_OUT; popMail.Connect(MAIL_POP, HtmlTextPath.EMAIL_PORT, true); popMail.Login(MAIL_USER_NAME, MAIL_USER_PWD); } Mail_Message mailMessage = null; try { if (message != null) { byte[] messBytes = message.MessageToByte(); mailMessage = Mail_Message.ParseFromByte(messBytes); } else { continue; } } catch (Exception) { SetRichTextValue(DateTime.Now.ToString("o") + @"ERR GET MSG TO BYTE"); continue; //Console.WriteLine(@"ERR GET MSG TO BYTE:" + DateTime.Now.ToString("o")); //throw; } sendmail = mailMessage.From[0].Address; HtmlBody = mailMessage.BodyHtmlText; if (!sendmail.Equals(MAIL_SENDER)) { message.MarkForDeletion(); SetRichTextValue(DateTime.Now.ToString("o") + @"######===" + sendmail + "===Message discarded#####"); continue; } //PrtOrder(HtmlBody.Replace("脳", "×").Replace("拢", "£")); //PrtOrderWithTemplate(HtmlBody); if (VERSION.Equals("2")) { HtmlBody = HtmlBody.Replace("<body style=\"", "<body style=\"font-family:Arial; "); } else if (VERSION.Equals("3")) { HtmlBody = HtmlBody.Replace("<body", "<body style=\"font-family:Arial; \""); } //读取Html失败时,跳出循环 if (!GetPrtInfo(HtmlBody)) { message.MarkForDeletion(); SetRichTextValue(DateTime.Now.ToString("o") + @"######===" + orderId + "===OLD Message discarded#####"); continue; } ////DataGridView中的订单号不能重复 //for (int j = 0; j < dgvOrder.RowCount; j++) //{ // if (dgvOrder.Rows[j].Cells[0].Value != null) // { // if (!string.IsNullOrEmpty(dgvOrder.Rows[j].Cells[0].Value.ToString())) // { // if (dgvOrder.Rows[j].Cells[0].Value.ToString().Equals(orderId)) // { // message.MarkForDeletion(); // SetRichTextValue(DateTime.Now.ToString("o") + @"######===" + orderId + "===DGV Message discarded#####"); // continue; // } // } // } //} //存在订单时不打印 //if (SqlHelper.QueryId(@"SELECT mailID FROM Mail_ID WHERE orderID='" + orderId + "'")) //{ // SetRichTextValue(DateTime.Now.ToString("o") + @"######Duplicate order ID######"); // continue; //} if (VERSION.Equals("2")) { HtmlBody = HtmlBody.Replace("h1", "h4").Replace("<p>", "").Replace("</p>", "<br />") .Replace("<p style=\"width:94%;\">", "").Replace("<strong>", "").Replace("</strong>", ""); //HtmlBody = HtmlBody.Replace("h1", "h5"); HtmlBody = HtmlBody.Replace("<h4>", "").Replace("</h4>", "").Replace("<b>", "") .Replace("</b>", "") .Replace("border-top:hidden;", "").Replace("style=\"border-top:hidden;\"", ""); } else if (VERSION.Equals("3")) { //中文字体更大一号字体 HtmlBody = HtmlBody.Replace("<span style=\"font-size:18px;\">", "<span style=\"font-size:24px;\">"); HtmlBody = HtmlBody.Replace("h1", "h4").Replace("<p>", "").Replace("</p>", "<br />") .Replace("<p style=\"width:94%;\">", "").Replace("<strong>", "").Replace("</strong>", ""); //HtmlBody = HtmlBody.Replace("h1", "h5"); HtmlBody = HtmlBody.Replace("<h4>", "").Replace("</h4>", "").Replace("<b>", "") .Replace("</b>", "") .Replace("border-top:hidden;", "").Replace("style=\"border-top:hidden;\"", ""); } //打印完成后插入数据 if (!SqlHelper.InsertId( @"INSERT INTO Mail_ID(mailID, orderID, orderType, orderTime, orderHtmlBody) VALUES('" + mail.UID + "', '" + orderId + "', '" + orderType + "', '" + orderDate + "', '')")) { MessageBox.Show(@"WRITE Data Error!", @"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { //int index = this.dgvOrder.Rows.Add(); this.dgvOrder.Rows.Insert(0, dgvOrder.Rows); dgvOrder.Rows[0].Cells[0].Value = orderId; dgvOrder.Rows[0].Cells[1].Value = orderDate; dgvOrder.Rows[0].Cells[2].Value = orderType; dgvOrder.Rows[0].Cells[3].Value = HtmlBody; dgvOrder.Refresh(); } //播放语音提示 if (File.Exists(Environment.CurrentDirectory + wavNewMail)) { SoundPlayer player = new SoundPlayer(Environment.CurrentDirectory + wavNewMail); player.Play(); } SetRichTextValue(@"#Time Printing order number=" + orderId); PrtContent(HtmlBody); //完成后添加删除邮件 lstMessage.Add(message); } for (int i = lstMessage.Count - 1; i >= 0; i--) { try { lstMessage[i].MarkForDeletion(); Console.WriteLine(@"DELETE MAIL DONE:" + DateTime.Now.ToString("o")); } catch (Exception) { Console.WriteLine(@"DELETE MAIL FAILURE:" + DateTime.Now.ToString("o")); //throw; } } if (popMail != null) { //popMail = null; try { popMail.Disconnect(); } catch (Exception) { //Console.Out.WriteLine("Error if (popMail != null)"); SetRichTextValue(@"Error POPMail != null"); } } } else { try { SetRichTextValue(DateTime.Now.ToString("o") + @"#No mail received#"); popMail.Disconnect(); } catch (Exception) { //Console.Out.WriteLine("Error else"); SetRichTextValue(@"Error ELSE"); } } }
public override void Pop3Close() { _pop3Client.Disconnect(); _pop3Client.Dispose(); }