public void Disconnect() { if (_popClient != null) { _popClient.Dispose(); _popClient = null; } }
private void m_pConnect_Click(object sender, EventArgs e) { if (m_pUserName.Text == "") { MessageBox.Show(this, "Please fill user name !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } POP3_Client pop3 = new POP3_Client(); try{ pop3.Logger = new Logger(); pop3.Logger.WriteLog += m_pLogCallback; pop3.Connect(m_pServer.Text, (int)m_pPort.Value, (m_pSecurity.SelectedIndex == 2)); if (m_pSecurity.SelectedIndex == 1) { pop3.Stls(null); } pop3.Login(m_pUserName.Text, m_pPassword.Text); m_pPop3 = pop3; this.DialogResult = DialogResult.OK; this.Close(); } catch (Exception x) { MessageBox.Show(this, "POP3 server returned: " + x.Message + " !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error); pop3.Dispose(); } }
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); }
/// <summary> /// Cleans up any resources being used. /// </summary> /// <param name="disposing"></param> protected override void Dispose(bool disposing) { base.Dispose(disposing); // Clean up POP3 client. if (m_pPop3 != null) { m_pPop3.Dispose(); m_pPop3 = null; } }
public override void Pop3Close() { _pop3Client.Disconnect(); _pop3Client.Dispose(); }
public void AnalysisLog_Thread() { DateTime dt_start = DatePicker_Start.Value; DateTime dt_end = DatePicker_End.Value; if (dt_start.Date > dt_end.Date) { return; } ListView[] all_item = new ListView[12]; for (int item_index = 0; item_index < 12; item_index++) { all_item[item_index] = new ListView(); } POP3_Client pop3 = new POP3_Client(); try { pop3.Connect("pop.qiye.163.com", WellKnownPorts.POP3, false); pop3.Login("*****@*****.**", "Lxrs1243"); m_pPop3 = pop3; try { int count = 0; this.Invoke(new Action(delegate() //多线程的处理 { timer1.Enabled = true; time_count = 0; toolStripProgressBar1.Value = 0; toolStripProgressBar1.Minimum = 0; toolStripProgressBar1.Maximum = m_pPop3.Messages.Count; toolStripProgressBar1.Step = 1; })); foreach (POP3_ClientMessage message in m_pPop3.Messages) { this.Invoke(new Action(delegate() //多线程的处理 { toolStripProgressBar1.Value++; })); Mail_Message mime = Mail_Message.ParseFromByte(message.HeaderToByte()); ListViewItem item = new ListViewItem(); if (string.IsNullOrEmpty(mime.Subject) || !mime.Subject.Contains("工作日")) { continue; } if (mime.Date > dt_start.Date && mime.Date < dt_end.AddDays(1).Date) { item.Text = mime.Subject.ToString(); //1.subject item.SubItems.Add(mime.Date.ToString()); //2.date } else { continue; } if (mime.From != null) //3.sender { string addr = mime.From.ToString(); string show_name = addr.Substring(addr.IndexOf("<") + 1, addr.IndexOf(">") - addr.IndexOf("<") - 1); bool exist = false; foreach (KeyValuePair <string, string> kvp in config._names) { if (kvp.Value.Equals(show_name)) { item.SubItems.Add(kvp.Key); exist = true; break; } } if (!exist) { item.SubItems.Add(show_name); } } else { item.SubItems.Add("<none>"); } item.SubItems.Add(((decimal)(message.Size / (decimal)1000)).ToString("f2") + " kb"); //4. size Mail_Message mimeBody = Mail_Message.ParseFromByte(message.MessageToByte()); if (mimeBody.BodyText != null) { item.SubItems.Add(mimeBody.BodyText); //5.content } else { mimeBody = Mail_Message.ParseFromByte(message.MessageToByte()); string content = null; this.Invoke(new Action(delegate() //多线程的处理 { HtmlToText convert = new HtmlToText(); content = convert.Convert(mimeBody.BodyHtmlText); })); item.SubItems.Add(content); //5.content } string date_time = item.SubItems[1].Text; int index = date_time.IndexOf("/"); string month = date_time.Substring(index + 1, date_time.IndexOf("/", index + 1) - index - 1); month.Trim(); all_item[Convert.ToInt16(month) - 1].Items.Add(item); if (count > 10) { break; } //count++; } } catch (Exception x) { MessageBox.Show(this, "Error: " + x.Message, "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception x) { MessageBox.Show(this, "POP3 server returned: " + x.Message + " !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error); pop3.Dispose(); } Excel.Application oXL; Excel._Workbook oWB; Excel._Worksheet oSheet; Excel.Range oRng; try { oXL = new Excel.Application(); oXL.Visible = true; oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value)); int month_count = 0; int month = 0; for (; month < 12; month++) { if (all_item[month].Items.Count == 0) { continue; } month_count++; if (month_count <= 3) { oSheet = (Excel._Worksheet)oWB.Worksheets[month_count]; } else { oSheet = oWB.Worksheets.Add(); } //oSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible; oSheet.Activate(); oSheet.Name = (month + 1).ToString() + "月份"; oRng = oSheet.get_Range("A1:BA1", Missing.Value); if (TB_DAILYW.Text != "") { oRng.ColumnWidth = Convert.ToInt16(TB_DAILYW.Text); } else { oRng.ColumnWidth = 40; } // oRng.Interior.ColorIndex = 35; oRng = oSheet.get_Range("A1:A30", Missing.Value); if (TB_DAILYH.Text != "") { oRng.RowHeight = Convert.ToInt16(TB_DAILYH.Text); } else { oRng.RowHeight = 25; } // oRng.Interior.ColorIndex = 36; oRng.Font.Bold = true; oRng = oSheet.get_Range("A1:BA50", Missing.Value); oRng.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft; // oRng.Borders.Weight = Excel.XlBorderWeight.xlThin; oRng = oSheet.get_Range("B2:BA50", Missing.Value); oRng.VerticalAlignment = Excel.XlVAlign.xlVAlignTop; int iRow = 1, iColumn = 1; ArrayList send_name = new ArrayList(); ArrayList send_date = new ArrayList(); string name = null; send_name.Add("No1"); send_date.Add("No1"); foreach (ListViewItem item in all_item[month].Items) { for (int i = 0; i < item.SubItems.Count; i++) { if (i == 1) //date { string date_time = item.SubItems[1].Text; string date = date_time.Substring(0, date_time.IndexOf(":") - 2).Trim(); date.Trim(); bool date_exist = false; for (int j = 0; j < send_date.Count; j++) { if (send_date[j].ToString() == date) { date_exist = true; iColumn = j + 1; } } if (!date_exist) { send_date.Add(date); iColumn = send_date.Count; } oSheet.Cells[1, iColumn] = date; } if (i == 2) //sender { name = item.SubItems[2].Text.ToString().Trim(); bool name_exist = false; for (int j = 0; j < send_name.Count; j++) { if (send_name[j].ToString() == name) { name_exist = true; iRow = j + 1; } } if (!name_exist) { send_name.Add(name); iRow = send_name.Count; } oSheet.Cells[iRow, 1] = name.Replace(" ", ""); } if (i == 4) //content { string content = item.SubItems[4].Text; if (content.Contains(name)) { content = content.Substring(0, content.IndexOf(name)); } if (content.Contains(name.Substring(0, 1) + " " + name.Substring(1))) { content = content.Substring(0, content.IndexOf(name.Substring(0, 1) + " " + name.Substring(1))); } string[] words = null; this.Invoke(new Action(delegate() { words = richTextBox1.Text.Split(';'); for (int index = 0; index < words.Count(); index++) { if (words[index] != "") { content = content.Replace(words[index], ""); } } })); string[] sentence = null; if (!content.Contains("\r\n") && content.Contains("\n")) { sentence = Regex.Split(content, "\n"); } else { sentence = Regex.Split(content, "\r\n"); } content = ""; foreach (string sen in sentence) { const string pattern = @"[~ ,,::.\.><]"; //删除数字和小数点,其他保留 Regex rx = new Regex(pattern); string result = rx.Replace(sen, ""); if (result != "") { content += sen.Trim() + "\r\n"; } } oSheet.Cells[iRow, iColumn] = content; } } } oXL.UserControl = true; } } catch (Exception theException) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } timer1.Enabled = false; }