⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop3.cs

📁 一个采用VS2008+Sql2000开发的任务管理系统
💻 CS
字号:
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Data;
using System.Web.Mail;

namespace BronzeMonkey
{
	/// <summary>
	/// Summary description for class POP3.
	/// </summary>
	public class Pop3
	{
		public int NumberOfMessages = 0;
		public int SizeOfMessages = 0;

		private TcpClient Pop3Server;
		private string _ServerUrl = String.Empty;
		private string _Username = String.Empty;
		private string _Password = String.Empty;
		private int _PortNumber = 110;
		
		private const string CRLF = "\r\n";

		public Pop3(string ServerUrl, int PortNumber, string Username, string Password)
		{
			_ServerUrl = ServerUrl;
			_PortNumber = PortNumber;
			_Username = Username;
			_Password = Password;
			Pop3Server = new TcpClient();
			Pop3Server.ReceiveTimeout = 10000;
		}

		public void Connect()
		{
			string Response = String.Empty;
			NetworkStream CurrentStream;
			
			Pop3Server.Connect(_ServerUrl, _PortNumber);

			CurrentStream = Pop3Server.GetStream();
			Response = GetResponse(CurrentStream);
			Response = SendCommand(CurrentStream, "user " + _Username + CRLF);
			Response = SendCommand(CurrentStream, "pass " + _Password + CRLF);

			if (Response.Substring(0, 4) == "-ERR")
				throw new Exception("An error occured while logging in to the POP3 Server - " + Response);
		}

		public void Disconnect()
		{
			string Response = String.Empty;
			NetworkStream CurrentStream = Pop3Server.GetStream();
			Response = SendCommand(CurrentStream, "QUIT" + CRLF);
			CurrentStream.Close();
			Pop3Server.Close();
		}

		private string SendCommand(NetworkStream CurrentStream, string Command)
		{
			byte[] CommandBytes = Encoding.ASCII.GetBytes(Command);
			CurrentStream.Write(CommandBytes, 0, CommandBytes.Length);
			return GetResponse(CurrentStream);
		}

		private string GetResponse(NetworkStream CurrentStream)
		{
			byte[] Buffer = new byte[Pop3Server.ReceiveBufferSize];
			string BufferString = String.Empty;
			string BufferLine = String.Empty;
			StreamReader ReadStream = new StreamReader(Pop3Server.GetStream());

			BufferString = ReadStream.ReadLine();

			if (BufferString.Substring(0, 1) != "-" && BufferString.Length > 1)
			{
				while (ReadStream.Peek() > -1)
				{
					BufferLine = ReadStream.ReadLine();
					BufferString += BufferLine + CRLF;
				}
			}
			else
				throw new Exception("An error occured while retrieving the response. " + BufferString);

			return BufferString;
		}

		/// <summary>
		/// Retreives the number of messages in the user's mailbox
		/// </summary>
  	public int GetMessageCount()
		{
			NetworkStream CurrentStream;
			string Response = String.Empty;

			CurrentStream = Pop3Server.GetStream();
			Response = SendCommand(CurrentStream, "stat" + CRLF);
			
			string[] MessageInfo = Response.Split(" ".ToCharArray());
			this.NumberOfMessages = Convert.ToInt32(MessageInfo[1]);
			this.SizeOfMessages	 = Convert.ToInt32(MessageInfo[2]);

			return this.NumberOfMessages;
		}

		/// <summary>
		/// Retreives a message corresponding to the message number given
		/// </summary>
		public MailMessage GetMessage(int MessageID)
		{
			MailMessage msg = new MailMessage();
			string[] MessageLines;
			NetworkStream CurrentStream;
			string Response = String.Empty;

			CurrentStream = Pop3Server.GetStream();

			Response = SendCommand(CurrentStream, "RETR " + MessageID.ToString() + CRLF);
			Response = GetResponse(CurrentStream);

			msg.BodyEncoding = System.Text.Encoding.ASCII;
			msg.BodyFormat = MailFormat.Text;
			msg.Subject = String.Empty; 
			msg.From = String.Empty;
			MessageLines = Response.Split(CRLF.ToCharArray());
			for (int i = 0; i < MessageLines.Length; i++)
			{
				if (msg.Subject == String.Empty || msg.From == String.Empty)
				{
					if (MessageLines[i].StartsWith("Subject:"))
						msg.Subject = MessageLines[i].Remove(0, 8);
					else if(MessageLines[i].StartsWith("From:"))
						msg.From = MessageLines[i].Remove(0, 5);
				}
				else
					msg.Body += MessageLines[i] + CRLF;
			}
			
			return msg;
		}

		/// <summary>
		/// Retrieves the user's entire mailbox in a dataset
		/// </summary>
		/// <returns></returns>
		public DataSet GetMailbox()
		{
			int MessageCount = GetMessageCount();
			MailMessage Message;

			DataSet ds = new DataSet();
			DataTable dt = new DataTable("Mailbox");
			dt.Columns.Add("MessageID", typeof(int));
			dt.Columns.Add("Subject", typeof(string));
			dt.Columns.Add("FromAddress", typeof(string));
			dt.Columns.Add("ReceivedDate", typeof(System.DateTime));
			dt.Columns.Add("Body", typeof(string));

			for (int i = 1; i <= MessageCount; i++)
			{
				Message = GetMessage(i);
				DataRow dr = dt.NewRow();
				dr["MessageID"] = i;
				dr["Subject"] = Message.Subject;
				dr["FromAddress"] = Message.From;
				dr["ReceivedDate"] = DateTime.Now;
				dr["Body"] = Message.Body;
				dt.Rows.Add(dr);
			}
			
			ds.Tables.Add(dt);
			return ds;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -