using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Collections; namespace ConsoleApplication1
...{
public class Pop3Server
...{
常量区#region 常量区
private const string COMMAND_DELE = "dele ";
private const string COMMAND_RETR = "retr ";
private const string COMMAND_SEP = " ";
private const string VALUE_USER = "user ";
private const string VALUE_PASS = "pass ";
private const string VALUE_ERR = "-ERR";
private const int DEFUALT_PORT = 110;
private const string DEFUALT_HOST = "localhost";
#endregion 变量区#region 变量区
private string _sHost;
private string _sPassWord;
private string _sUserName;
private int _iPort;
private string _sErrorMsg = "";
private TcpClient _tcpClient;
private NetworkStream ns;
#endregion 属性区#region 属性区
/**//// <summary>
/// 邮件服务器名称 defual laocalhost
/// </summary>
public string Host
...{
set
...{
_sHost = value;
}
}
/**//// <summary>
/// 用户密码
/// </summary>
public string PassWord
...{
set
...{
_sPassWord = value;
}
}
/**//// <summary>
/// 用户名
/// </summary>
public string UserName
...{
set
...{
_sUserName = value;
}
}
/**//// <summary>
/// 端口,defualt 110
/// </summary>
public int Port
...{
set
...{
_iPort = value;
}
}
/**//// <summary>
/// 错误信息
/// </summary>
public string ErrorMsg
...{
get
...{
return _sErrorMsg;
}
}
#endregion 构造器#region 构造器
/**//// <summary>
/// 构造器,要求服务器和端口
/// </summary>
/// <param name="sUserName"> 用户名 </param>
/// <param name="sPassWord"> 密码 </param>
/// <param name="sHost"> 服务器名称 </param>
/// <param name="iPort"> 端口号 </param>
public Pop3Server(string sUserName, string sPassWord, string sHost, int iPort)
...{
_sUserName = sUserName;
_sPassWord = sPassWord;
_sHost = sHost;
_iPort = iPort;
}
/**//// <summary>
/// 构造器,使用默认端口号、服务器
/// </summary>
/// <param name="sUserName"> </param>
/// <param name="sPassWord"> </param>
public Pop3Server(string sUserName, string sPassWord)
: this(sUserName, sPassWord, DEFUALT_HOST, DEFUALT_PORT)
...{
//
}
#endregion 可访问方法#region 可访问方法
/**//// <summary>
/// 连结到服务器,成功返回真
/// </summary>
/// <returns> 成功返回真 </returns>
public Boolean DoConnect()
...{
try
...{
_tcpClient = new TcpClient(_sHost, _iPort);
byte[] outbytes;
string input;
ns = _tcpClient.GetStream();
StreamReader sr = new StreamReader(ns); sr.ReadLine();
input = VALUE_USER + _sUserName + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine(); input = VALUE_PASS + _sPassWord + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true;
}
catch (SocketException ex)
...{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
//
}
/**//// <summary>
/// 删除邮件,成功返回真
/// </summary>
/// <param name="iIndex"> 邮件序号 </param>
/// <returns> 成功返回真 </returns>
public Boolean DoDeleteMail(int iIndex)
...{
Byte[] outbytes;
string input;
try
...{
input = COMMAND_DELE + iIndex.ToString() + COMMAND_SEP;
outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
}
catch (SocketException ex)
...{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
return true; }
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Collections; namespace ConsoleApplication1
...{
public class Pop3Server
...{
常量区#region 常量区
private const string COMMAND_DELE = "dele ";
private const string COMMAND_RETR = "retr ";
private const string COMMAND_SEP = " ";
private const string VALUE_USER = "user ";
private const string VALUE_PASS = "pass ";
private const string VALUE_ERR = "-ERR";
private const int DEFUALT_PORT = 110;
private const string DEFUALT_HOST = "localhost";
#endregion 变量区#region 变量区
private string _sHost;
private string _sPassWord;
private string _sUserName;
private int _iPort;
private string _sErrorMsg = "";
private TcpClient _tcpClient;
private NetworkStream ns;
#endregion 属性区#region 属性区
/**//// <summary>
/// 邮件服务器名称 defual laocalhost
/// </summary>
public string Host
...{
set
...{
_sHost = value;
}
}
/**//// <summary>
/// 用户密码
/// </summary>
public string PassWord
...{
set
...{
_sPassWord = value;
}
}
/**//// <summary>
/// 用户名
/// </summary>
public string UserName
...{
set
...{
_sUserName = value;
}
}
/**//// <summary>
/// 端口,defualt 110
/// </summary>
public int Port
...{
set
...{
_iPort = value;
}
}
/**//// <summary>
/// 错误信息
/// </summary>
public string ErrorMsg
...{
get
...{
return _sErrorMsg;
}
}
#endregion 构造器#region 构造器
/**//// <summary>
/// 构造器,要求服务器和端口
/// </summary>
/// <param name="sUserName"> 用户名 </param>
/// <param name="sPassWord"> 密码 </param>
/// <param name="sHost"> 服务器名称 </param>
/// <param name="iPort"> 端口号 </param>
public Pop3Server(string sUserName, string sPassWord, string sHost, int iPort)
...{
_sUserName = sUserName;
_sPassWord = sPassWord;
_sHost = sHost;
_iPort = iPort;
}
/**//// <summary>
/// 构造器,使用默认端口号、服务器
/// </summary>
/// <param name="sUserName"> </param>
/// <param name="sPassWord"> </param>
public Pop3Server(string sUserName, string sPassWord)
: this(sUserName, sPassWord, DEFUALT_HOST, DEFUALT_PORT)
...{
//
}
#endregion 可访问方法#region 可访问方法
/**//// <summary>
/// 连结到服务器,成功返回真
/// </summary>
/// <returns> 成功返回真 </returns>
public Boolean DoConnect()
...{
try
...{
_tcpClient = new TcpClient(_sHost, _iPort);
byte[] outbytes;
string input;
ns = _tcpClient.GetStream();
StreamReader sr = new StreamReader(ns); sr.ReadLine();
input = VALUE_USER + _sUserName + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine(); input = VALUE_PASS + _sPassWord + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true;
}
catch (SocketException ex)
...{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
//
}
/**//// <summary>
/// 删除邮件,成功返回真
/// </summary>
/// <param name="iIndex"> 邮件序号 </param>
/// <returns> 成功返回真 </returns>
public Boolean DoDeleteMail(int iIndex)
...{
Byte[] outbytes;
string input;
try
...{
input = COMMAND_DELE + iIndex.ToString() + COMMAND_SEP;
outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
}
catch (SocketException ex)
...{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
return true; }
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Collections; namespace ConsoleApplication1
...{
public class Pop3Server
...{
常量区#region 常量区
private const string COMMAND_DELE = "dele ";
private const string COMMAND_RETR = "retr ";
private const string COMMAND_SEP = " ";
private const string VALUE_USER = "user ";
private const string VALUE_PASS = "pass ";
private const string VALUE_ERR = "-ERR";
private const int DEFUALT_PORT = 110;
private const string DEFUALT_HOST = "localhost";
#endregion 变量区#region 变量区
private string _sHost;
private string _sPassWord;
private string _sUserName;
private int _iPort;
private string _sErrorMsg = "";
private TcpClient _tcpClient;
private NetworkStream ns;
#endregion 属性区#region 属性区
/**//// <summary>
/// 邮件服务器名称 defual laocalhost
/// </summary>
public string Host
...{
set
...{
_sHost = value;
}
}
/**//// <summary>
/// 用户密码
/// </summary>
public string PassWord
...{
set
...{
_sPassWord = value;
}
}
/**//// <summary>
/// 用户名
/// </summary>
public string UserName
...{
set
...{
_sUserName = value;
}
}
/**//// <summary>
/// 端口,defualt 110
/// </summary>
public int Port
...{
set
...{
_iPort = value;
}
}
/**//// <summary>
/// 错误信息
/// </summary>
public string ErrorMsg
...{
get
...{
return _sErrorMsg;
}
}
#endregion 构造器#region 构造器
/**//// <summary>
/// 构造器,要求服务器和端口
/// </summary>
/// <param name="sUserName"> 用户名 </param>
/// <param name="sPassWord"> 密码 </param>
/// <param name="sHost"> 服务器名称 </param>
/// <param name="iPort"> 端口号 </param>
public Pop3Server(string sUserName, string sPassWord, string sHost, int iPort)
...{
_sUserName = sUserName;
_sPassWord = sPassWord;
_sHost = sHost;
_iPort = iPort;
}
/**//// <summary>
/// 构造器,使用默认端口号、服务器
/// </summary>
/// <param name="sUserName"> </param>
/// <param name="sPassWord"> </param>
public Pop3Server(string sUserName, string sPassWord)
: this(sUserName, sPassWord, DEFUALT_HOST, DEFUALT_PORT)
...{
//
}
#endregion 可访问方法#region 可访问方法
/**//// <summary>
/// 连结到服务器,成功返回真
/// </summary>
/// <returns> 成功返回真 </returns>
public Boolean DoConnect()
...{
try
...{
_tcpClient = new TcpClient(_sHost, _iPort);
byte[] outbytes;
string input;
ns = _tcpClient.GetStream();
StreamReader sr = new StreamReader(ns); sr.ReadLine();
input = VALUE_USER + _sUserName + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine(); input = VALUE_PASS + _sPassWord + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true;
}
catch (SocketException ex)
...{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
//
}
/**//// <summary>
/// 删除邮件,成功返回真
/// </summary>
/// <param name="iIndex"> 邮件序号 </param>
/// <returns> 成功返回真 </returns>
public Boolean DoDeleteMail(int iIndex)
...{
Byte[] outbytes;
string input;
try
...{
input = COMMAND_DELE + iIndex.ToString() + COMMAND_SEP;
outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
}
catch (SocketException ex)
...{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
return true; }
只能贴一部分。// POP3 Client
// ===========
//
// copyright by Peter Huber, Singapore, 2006
// this code is provided as is, bugs are probable, free for any use at own risk, no
// responsibility accepted. All rights, title and interest in and to the accompanying content retained. :-)
//
// based on POP3 Client as a C# Class, by Bill Dean, http://www.codeproject.com/csharp/Pop3MailClient.asp
// based on Retrieve Mail From a POP3 Server Using C#, by Agus Kurniawan, http://www.codeproject.com/csharp/popapp.asp
// based on Post Office Protocol - Version 3, http://www.ietf.org/rfc/rfc1939.txtusing System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Text;
namespace Pop3 {
// Supporting classes and structs
// ============================== /// <summary>
/// Combines Email ID with Email UID for one email
/// The POP3 server assigns to each message a unique Email UID, which will not change for the life time
/// of the message and no other message should use the same.
///
/// Exceptions:
/// Throws Pop3Exception if there is a serious communication problem with the POP3 server, otherwise
///
/// </summary>
public struct EmailUid {
/// <summary>
/// used in POP3 commands to indicate which message (only valid in the present session)
/// </summary>
public int EmailId;
/// <summary>
/// Uid is always the same for a message, regardless of session
/// </summary>
public string Uid; /// <summary>
/// constructor
/// </summary>
public EmailUid(int EmailId, string Uid) {
this.EmailId = EmailId;
this.Uid = Uid;
}
}
/// <summary>
/// If anything goes wrong within Pop3MailClient, a Pop3Exception is raised
/// </summary>
public class Pop3Exception:ApplicationException {
/// <summary>
/// Pop3 exception with no further explanation
/// </summary>
public Pop3Exception() { }
/// <summary>
/// Pop3 exception with further explanation
/// </summary>
public Pop3Exception(string ErrorMessage) : base(ErrorMessage) { }
}
/// <summary>
/// A pop 3 connection goes through the following states:
/// </summary>
public enum Pop3ConnectionStateEnum {
/// <summary>
/// undefined
/// </summary>
None=0,
/// <summary>
/// not connected yet to POP3 server
/// </summary>
Disconnected,
/// <summary>
/// TCP connection has been opened and the POP3 server has sent the greeting. POP3 server expects user name and password
/// </summary>
Authorization,
/// <summary>
/// client has identified itself successfully with the POP3, server has locked all messages
/// </summary>
Connected,
/// <summary>
/// QUIT command was sent, the server has deleted messages ed for deletion and released the resources
/// </summary>
Closed
}
// Delegates for Pop3MailClient
// ============================ /// <summary>
/// If POP3 Server doesn't react as expected or this code has a problem, but
/// can continue with the execution, a Warning is called.
/// </summary>
/// <param name="WarningText"></param>
/// <param name="Response">string received from POP3 server</param>
public delegate void WarningHandler(string WarningText, string Response);
/// <summary>
/// Traces all the information exchanged between POP3 client and POP3 server plus some
/// status messages from POP3 client.
/// Helpful to investigate any problem.
/// Console.WriteLine() can be used
/// </summary>
public delegate void TraceHandler(string TraceText);
// Pop3MailClient Class
// ==================== /// <summary>
/// provides access to emails on a POP3 Server
/// </summary>
public class Pop3MailClient { //Events
//------ /// <summary>
/// Called whenever POP3 server doesn't react as expected, but no runtime error is thrown.
/// </summary>
public event WarningHandler Warning; /// <summary>
/// call warning event
/// </summary>
/// <param name="methodName">name of the method where warning is needed</param>
/// <param name="response">answer from POP3 server causing the warning</param>
/// <param name="warningText">explanation what went wrong</param>
/// <param name="warningParameters"></param>
protected void CallWarning(string methodName, string response, string warningText, params object[] warningParameters) {
try {
warningText = string.Format(warningText, warningParameters);
} catch {
}
if (Warning!=null) {
Warning(methodName + ": " + warningText, response);
}
CallTrace("!! {0}", warningText);
}
/// <summary>
/// Shows the communication between PopClient and PopServer, including warnings
/// </summary>
public event TraceHandler Trace; /// <summary>
/// call Trace event
/// </summary>
/// <param name="text">string to be traced</param>
/// <param name="parameters"></param>
protected void CallTrace(string text, params object[] parameters) {
if (Trace!=null) {
Trace(DateTime.Now.ToString("hh:mm:ss ") + popServer + " " + string.Format(text, parameters));
}
} /// <summary>
/// Trace information received from POP3 server
/// </summary>
/// <param name="text">string to be traced</param>
/// <param name="parameters"></param>
protected void TraceFrom(string text, params object[] parameters) {
if (Trace!=null) {
CallTrace(" " + string.Format(text, parameters));
}
}
//Properties
//---------- /// <summary>
/// Get POP3 server name
/// </summary>
public string PopServer {
get { return popServer; }
}
/// <summary>
/// POP3 server name
/// </summary>
protected string popServer;
c.Connect();
List<int> ids = new List<int>();
if (c.GetEmailIdList(out ids))
{
}
c.Disconnect();
这有下载