?? pop3connection.cs
字號:
using System;
using System.ComponentModel;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace Pop3Com
{
/// <summary>
/// Pop3 的摘要說明:Pop3類完成郵件的接收功能。
/// </summary>
public class Pop3: Component
{
private string _host = "127.0.0.1";
private int _port = 110;
private string _userName;
private string _passWord;
private int _numOfMails;
private double _totalSize;
private string _body;
private string _status;
private Pop3Connection con;
private const string CRLF = "\r\n";
private const string serverConfig = "服務器配置";
private const string reciveEvent = "接收事件";
public delegate void MailRecivedDelegate();
[Category(reciveEvent)] [Description("郵件接收成功觸發的事件")]
public event MailRecivedDelegate OnMailRecived;
[Category(serverConfig)]
public string Host
{
get{return _host;}
set
{
if (value == null || value.Trim().Length == 0)
{
throw new ArgumentException("無效的主機名");
}
_host = value;
}
}
[Category(serverConfig)]
public int Port
{
get{return _port;}
set
{
if (value <= 0)
{
throw new ArgumentException("無效的端口");
}
_port = value;
}
}
[Category(serverConfig)]
public string UserName
{
get{return _userName;}
set
{
if (value == null || value.Trim().Length == 0)
{
throw new ArgumentException("無效的用戶名");
}
_userName = value;
}
}
[Category(serverConfig)]
public string PassWord
{
get{return _passWord;}
set
{
if (value == null || value.Trim().Length == 0)
{
throw new ArgumentException("無效的密碼");
}
_passWord = value;
}
}
[Browsable(false)]
public int NumOfMails
{
get{return _numOfMails;}
}
[Browsable(false)]
public double TotalSize
{
get{return _totalSize;}
}
[Browsable(false)]
public string Body
{
get{return _body;}
}
[Browsable(false)]
public string Status
{
get{return _status;}
}
/// <summary>
/// 接收郵件信息
/// </summary>
public void ReciveMessage()
{
//避免線程沖突
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 i = 0; i < _numOfMails; i++)
{
//根據郵件編號從服務器獲得相應郵件
buf.Length = 0;
buf.Append("RETR");
buf.Append(i.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 (OnMailRecived != null)
{
OnMailRecived();
}
}
}
}
/// <summary>
/// Pop3Connection 的摘要說明:Pop3Connection類完成了與主機的連接、通訊和關閉連接等功能。
/// </summary>
public class Pop3Connection
{
private TcpClient socket;
private StreamReader reader;
private StreamWriter writer;
private bool _connected;
public bool Connected
{
get{return _connected;}
}
public Pop3Connection()
{
//
// TODO: 在此處添加構造函數邏輯
//
socket = new TcpClient();
}
public void Open(string host, int port)
{
if (host == null || host.Trim().Length == 0 || port <= 0)
{
throw new ArgumentException("參數值無效");
}
try
{
socket.Connect(host, port);
reader = new StreamReader(socket.GetStream(),System.Text.Encoding.ASCII);
writer = new StreamWriter(socket.GetStream(),System.Text.Encoding.ASCII);
_connected = true;
}
catch(ArgumentNullException)
{
throw new ArgumentException("主機名為空,無效的主機名");
}
catch(ArgumentOutOfRangeException)
{
throw new ArgumentException("端口號port不在主機的最大端口號和最小端口號之間");
}
catch(ObjectDisposedException)
{
throw new ArgumentException("主機TcpClient 被關閉");
}
}
internal void SendCommand(string cmd)
{
try
{
writer.WriteLine(cmd);
writer.Flush();
}
catch(Exception e)
{
throw new ArgumentException(e.Message);
}
}
internal void GetReply(out string reply, out int code)
{
try
{
reply = reader.ReadLine();
}
catch(Exception e)
{
throw new ArgumentException(e.Message);
}
code = -1;//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;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -