/// <summary> /// 获取知道获取到. 否则一直获取数据 /// </summary> /// <param name="p_Value"></param> /// <returns></returns> private byte[] ReadEnd(byte[] p_Value, POP3Client p_Client) { StringBuilder sb = new StringBuilder(); sb.Append(System.Text.Encoding.ASCII.GetString(p_Value).TrimEnd('\0')); //String tmp = System.Text.Encoding.ASCII.GetString(p_Value); if (sb.ToString().IndexOf("\r\n.\r\n") != -1) { return(p_Value); } //MemoryStream _Stream = new MemoryStream(); //_Stream.Write(p_Value, 0, p_Value.Length); while (true) { try { byte[] _WriteBytes = new byte[p_Client.Client.ReceiveBufferSize]; p_Client.Client.Client.Receive(_WriteBytes); //_Stream.Write(_WriteBytes, 0, _WriteBytes.Length); System.Threading.Thread.Sleep(100); sb.Append(System.Text.Encoding.ASCII.GetString(_WriteBytes).TrimEnd('\0')); if (sb.ToString().IndexOf("\r\n.\r\n") != -1) { return(System.Text.Encoding.ASCII.GetBytes(sb.ToString())); } } catch (System.Exception ex) { return(new byte[0]); } } }
/// <summary> /// 连接事件 /// </summary> /// <param name="ar"></param> private void OnSend(IAsyncResult ar) { POP3Client _Client = (POP3Client)ar.AsyncState; byte[] _ReadBytes = new byte[0]; _Client.Client.Client.BeginReceive(_ReadBytes, 0, 0, SocketFlags.None, new AsyncCallback(OnWrite), _Client); }
/// <summary> /// 是否登录过,记住了密码 /// </summary> /// <returns>登录过返回对象,否则返回null</returns> public static bool IsEverLoggedIn() { DirectoryInfo dir = new DirectoryInfo(SerializeUtil.Dir); if (!Directory.Exists(SerializeUtil.Dir)) { Directory.CreateDirectory(SerializeUtil.Dir); } FileInfo[] infos = dir.GetFiles(); //是否登录过 if (infos.Length != 0) //是 { foreach (FileInfo info in infos) { //获得用户信息 User user = SerializeUtil.DeSerializeUser(info.FullName); if (user.isLogin) { POP3Client pop3Client = new POP3Client(user); SMTPClient smtpClient = new SMTPClient(user); DataService.pop3 = pop3Client; DataService.smtp = smtpClient; return(true); } } } return(false); }
private void stopTimeoutTimer() { m_Client = null; if (m_TimeoutTimer != null) { m_TimeoutTimer.Stop(); m_TimeoutTimer.Close(); } }
/// <summary> /// 初始化超时计时器,因为socket的异步处理无法设定SendTimeout、ReceiveTimeout来启用超时机制 /// </summary> /// <param name="client"></param> private void initTimeoutTimer(POP3Client client) { m_Client = client; m_TimeoutTimer = new System.Timers.Timer(); m_TimeoutTimer.Interval = 60000; //超时一分钟 m_TimeoutTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeoutTimer_Elapsed); m_TimeoutTimer.Enabled = true; m_TimeoutTimer.AutoReset = false; }
/// <summary> /// 超时后执行的方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TimeoutTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (m_Client.Client != null) { m_Client.ReturnEnd = true; m_Client.ReadEnd = true; m_Client.Error = "timeout"; m_Client.Client.Close(); m_Client = null; } }
/// <summary> /// 连接事件 /// </summary> /// <param name="ar"></param> private void OnWrite(IAsyncResult ar) { POP3Client _Client = (POP3Client)ar.AsyncState; byte[] _WriteBytes = new byte[_Client.Client.Client.ReceiveBufferSize]; _Client.Client.Client.Receive(_WriteBytes); if (_Client.ReadEnd) { _WriteBytes = ReadEnd(_WriteBytes, _Client); } byte[] _SendBytes = _Client.GetSendBytes(_WriteBytes); if (_SendBytes.Length == 0) { return; } _Client.Client.Client.BeginSend(_SendBytes, 0, _SendBytes.Length, SocketFlags.None, new AsyncCallback(OnSend), _Client); }
/// <summary> /// 获取Mail列表 /// </summary> /// <param name="p_Name">用户名</param> /// <param name="p_PassWord">密码</param> /// <returns>Mail信息</returns> public DataTable GetMailTable(string p_Name, string p_PassWord) { POP3Client _Client = new POP3Client(); _Client.UserName = p_Name; _Client.PassWord = p_PassWord; _Client.Client = new TcpClient(); _Client.Client.BeginConnect(m_Address, m_Port, new AsyncCallback(OnConnectRequest), _Client); while (!_Client.ReturnEnd) { System.Windows.Forms.Application.DoEvents(); } if (_Client.Error.Length != 0) { throw new Exception("错误信息!" + _Client.Error); } return(_Client.MailDataTable); }
/// <summary> /// 登录POP3 /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public static bool LoginPOP3(User user, bool needSerialize) { POP3Client pop3Client = new POP3Client(user); //进行连接验证pop, 信息是否正确 if (pop3Client.Login(user)) //信息正确 { //返回用户对象 user.isLogin = true; if (needSerialize) { SerializeUtil.SerializeUser(user); } DataService.pop3 = pop3Client; DataService.isFirstLogin = true; return(true); } //信息错误 return(false); }
/// <summary> /// 获取知道获取到. 否则一直获取数据 /// </summary> /// <param name="p_Value"></param> /// <returns></returns> private byte[] ReadEnd(byte[] p_Value, POP3Client p_Client) { if (System.Text.Encoding.ASCII.GetString(p_Value).IndexOf("\r\n.\r\n") != -1) { return(p_Value); } MemoryStream _Stream = new MemoryStream(); _Stream.Write(p_Value, 0, p_Value.Length); while (true) { byte[] _WriteBytes = new byte[p_Client.Client.ReceiveBufferSize]; p_Client.Client.Client.Receive(_WriteBytes); _Stream.Write(_WriteBytes, 0, _WriteBytes.Length); System.Threading.Thread.Sleep(100); if (System.Text.Encoding.ASCII.GetString(_WriteBytes).IndexOf("\r\n.\r\n") != -1) { return(_Stream.ToArray()); } } }
static void POPTest() { mailhost host = new mailhost { server = "pop.126.com", Ssl = false, address = "*****@*****.**", password = "******", port = 110 }; using (POP3Client client = new POP3Client()) { try { // 建立连接 client.Connect(host.server, host.port, host.Ssl); // 登陆账号 client.Loging(host.address, host.password); //// 获取当前邮箱邮件个数 Int32 count = client.GetMailCount(); //client.DeleMail(1); //client.ResetStatus(); //client.Disconnect(); //// 获得邮件头列表 MailHeadList mailheaders = client.GetMailHeaders(); var model = client.GetMail(2); // 循环显示邮件信息 foreach (MailHeadModel mailheader in mailheaders) { Console.WriteLine("UID: " + mailheader.UID); Console.WriteLine("用户名: " + mailheader.Name); Console.WriteLine("地址: " + mailheader.Address); Console.WriteLine("主题: " + mailheader.Subject); Console.WriteLine("日期: " + mailheader.Date.ToString("yyyy-MM-dd")); Console.WriteLine(); } var mails = client.GetMails(); foreach (var mail in mails) { Console.WriteLine("UID: " + mail.MailHead.UID); Console.WriteLine("用户名: " + mail.MailHead.Name); Console.WriteLine("地址: " + mail.MailHead.Address); Console.WriteLine("主题: " + mail.MailHead.Subject); Console.WriteLine("日期: " + mail.MailHead.Date.ToString("yyyy-MM-dd")); Console.WriteLine("内容: " + mail.Body); Console.WriteLine(); Console.WriteLine("超文本内容: " + mail.BodyHtml); Console.WriteLine("附件:"); foreach (var item in mail.Attachments) { //item.Download(@"E:\mailattachments"); Console.WriteLine(item.Name); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); } client.Disconnect(); } catch (ArgumentException aex) { Console.WriteLine(aex); } catch (Exception ex) { Console.WriteLine(ex); } } Console.ReadLine(); }