| public bool Connected { get{return connected;} } Pop3Connection类的主要方法包含以下几个: internal void Open(string host, int port) { if(host == null || host.Trim().Length == 0 || port <= 0) { throw new System.ArgumentException("Invalid Argument found."); } socket.Connect(host, port); reader = new StreamReader(socket.GetStream(), System.Text.Encoding.ASCII); writer = new StreamWriter(socket.GetStream(), System.Text.Encoding.ASCII); connected = true; } internal void SendCommand(string cmd) { writer.WriteLine(cmd); writer.Flush(); } internal void GetReply(out string reply, out int code) { reply = reader.ReadLine(); code = reply == null ? -1 : Int32.Parse(reply.Substring(0, 3)); } internal void Close() { reader.Close(); writer.Flush(); writer.Close(); reader = null; writer = null; socket.Close(); connected = false; } |
| /// /// 主机名 /// public string Host { get {return host;} set { if(value == null || value.Trim().Length == 0) { throw new ArgumentException("Invalid host name."); } host = value; } } /// /// 端口号 /// public int Port { get {return port;} set { if(value <= 0) { throw new ArgumentException("Invalid port."); } port = value; } } /// /// 用户名 /// public string UserName { get {return username;} set { if(value == null || value.Trim().Length == 0) { throw new ArgumentException("Invalid user name."); } username = value; } } /// /// 密码 /// public string PassWord { get {return password;} set { if(value == null) { throw new ArgumentException("Invalid password."); } password = value; } } /// /// 邮件数量 /// public int NumOfMails { get {return numofmails;} } /// /// 邮件总体积 /// public double TotalSize { get {return totalsize;} } /// /// 邮件内容 /// public string Body { get {return body;} } /// /// 状态信息 /// public string Status { get {return status;} } |
| /// /// 接收邮件信息 /// public void ReceiveMessage() { // 避免线程冲突 lock(this) { // 设置初始连接 con = new Pop3Connection(); if(port <= 0) port = 110; con.Open(host, port); StringBuilder buf = new StringBuilder(); string response; int code; // 获取欢迎信息 con.GetReply(out response, out code); status += response; //登录服务器过程 buf.Append("USER"); buf.Append(username); buf.Append(CRLF); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); status += response; buf.Length = 0; buf.Append("PASS"); buf.Append(password); buf.Append(CRLF); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); status += response; //向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小 buf.Length = 0; buf.Append("STAT"); buf.Append(CRLF); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); status += response; //将总邮件数和邮件大小分离 string[] TotalStat = response.Split(new char[] {' '}); numofmails = Int32.Parse(TotalStat[1]); totalsize = (double)Int32.Parse(TotalStat[2]); for( int x = 0; x < numofmails; ++x) { //根据邮件编号从服务器获得相应邮件 buf.Length = 0; buf.Append("RETR"); buf.Append(x.ToString()); buf.Append(CRLF); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); if(response[0]!='-') { //不断地读取邮件内容,只到结束标志:英文句号 while(response!=".") { body += response; con.GetReply(out response, out code); } } else status += response; } //向服务器发送QUIT命令从而结束和POP3服务器的会话 buf.Length = 0; buf.Append("QUIT"); buf.Append(CRLF); con.SendCommand(buf.ToString()); con.GetReply(out response, out code); status += response; con.Close(); // 邮件接收成功后触发的事件 if(OnMailReceived != null) { OnMailReceived(); } } } |
| /// /// 通过一个独立的线程接收邮件 /// public void ReceiveMessageAsync() { new Thread(new ThreadStart(ReceiveMessage)).Start(); } |
| public delegate void MailReceivedDelegate(); |
| public event MailReceivedDelegate OnMailReceived; |

![]() |
| private void checkBtn_Click(object sender, System.EventArgs e) { // 正确性检查 if(host == null || host.Text.Trim().Length == 0) { MessageBox.Show("请填入服务器地址!"); } else if(username == null || username.Text.Trim().Length == 0) { MessageBox.Show("请填入用户名!"); } else if(password == null || password.Text.Trim().Length == 0) { MessageBox.Show("请填入密码!"); } else { mailer = new Pop3(); mailer.Host = host.Text; mailer.Port = Int32.Parse(port.Text); mailer.UserName = username.Text; mailer.PassWord = password.Text; statusBar.Text = "正在接收信息……"; mailer.OnMailReceived += new Pop3.MailReceivedDelegate(OnMailReceived); mailer.ReceiveMessageAsync(); } } |
| private void OnMailReceived() { statusBar.Text = "邮件接收完毕!"; MessageBox.Show("你有" + mailer.NumOfMails.ToString() + "个邮件!","信息", MessageBoxButtons.OK,MessageBoxIcon.Information); } |
![]() |