我也不懂,等待高人解答。好象要比较熟悉pop协议,自己写接受邮件的代码。
解决方案 »
- asp与asp.net共享session
- 100分 asp.net连接oracle数据库
- 接口问题
- VS2005调试过程CSS代码没有效果,Flash不能显示,朋友们进来看看帮帮忙,谢谢!
- 字母与数字混合的流水号及如何获取面页上所有CheckBox的ID
- 急!!!我刚刚装的.NET 1.0 ,英文版的。为什么好像不能解释服务器控件?
- [100分],请问“document.all.xxx.detachEvent”“event.srcElement.tagName ”这是什么技术或语言,我想学,请问该看什么书?
- 请问在哪儿能下载Windows组件更新哪?
- 如何批量生产简历?
- 关于数据库父子关系的节点转为json的问题?
- 请教一个枚举类型的问题!
- 关于DataGrid绑定的问题。怎么对DataField进行格式化。请帮我看这一句代码。谢谢 !
经常有人问这个问题
你搜.......
using System.IO;
using System.Net.Sockets;namespace Pop3Com
{
/// <summary>
/// Pop3Connection 的摘要说明。
/// </summary>
internal class Pop3Connection
{
// 成员变量
private TcpClient socket;
private StreamReader reader;
private StreamWriter writer;
private bool connected; /// <summary>
/// 连接状态
/// </summary>
public bool Connected
{
get{return connected;}
} /// <summary>
/// 创建一个新的连接
/// </summary>
internal Pop3Connection()
{
socket = new TcpClient();
} /// <summary>
/// 在默认的端口上打开一个和主机的连接
/// </summary>
/// <param name="host">要连接的主机名</param>
internal void Open(string host)
{
Open(host, 25);
}
/// <summary>
/// 在某端口上打开一个和主机的连接
/// </summary>
/// <param name="host">要连接的主机名</param>
/// <param name="port">要连接的端口号</param>
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;
} /// <summary>
/// 关闭连接
/// </summary>
internal void Close()
{
reader.Close();
writer.Flush();
writer.Close();
reader = null;
writer = null;
socket.Close();
connected = false;
} /// <summary>
/// 给主机发送一个命令
/// </summary>
/// <param name="cmd">要发送的命令</param>
internal void SendCommand(string cmd)
{
writer.WriteLine(cmd);
writer.Flush();
} /// <summary>
/// 给主机发送数据,用来发送附件
/// </summary>
/// <param name="buf">数据缓存</param>
/// <param name="start">数据缓存的开始点</param>
/// <param name="length">数据长度</param>
internal void SendData(char[] buf, int start, int length)
{
writer.Write(buf, start, length);
} /// <summary>
/// 从主机获取响应信息
/// </summary>
/// <param name="reply">从主机获取的文本信息</param>
/// <param name="code">从主机获取的状态码</param>
internal void GetReply(out string reply, out int code)
{
reply = reader.ReadLine();
code = reply == null ? -1 : Int32.Parse(reply.Substring(0, 3));
}
}
}
using System.Collections;
using System.Text;
using System.IO;
using System.Threading;namespace Pop3Com
{
/// <summary>
/// Pop3 的摘要说明。
/// </summary>
public class Pop3
{
/// <summary>
/// 邮件接收完毕通知的代理
/// </summary>
public delegate void MailReceivedDelegate(); /// <summary>
/// 邮件接收成功后的事件
/// </summary>
public event MailReceivedDelegate OnMailReceived; private Pop3Connection con;
private string host;
private int port;
private string username;
private string password;
private int numofmails;
private double totalsize;
private string body;
private string status;
private string CRLF = "\r\n";
#region Properties
/// <summary>
/// 主机名
/// </summary>
public string Host
{
get {return host;}
set
{
if(value == null || value.Trim().Length == 0)
{
throw new ArgumentException("Invalid host name.");
}
host = value;
}
} /// <summary>
/// 端口号
/// </summary>
public int Port
{
get {return port;}
set
{
if(value <= 0)
{
throw new ArgumentException("Invalid port.");
}
port = value;
}
}
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get {return username;}
set
{
if(value == null || value.Trim().Length == 0)
{
throw new ArgumentException("Invalid user name.");
}
username = value;
}
} /// <summary>
/// 密码
/// </summary>
public string PassWord
{
get {return password;}
set
{
if(value == null)
{
throw new ArgumentException("Invalid password.");
}
password = value;
}
} /// <summary>
/// 邮件数量
/// </summary>
public int NumOfMails
{
get {return numofmails;}
} /// <summary>
/// 邮件总体积
/// </summary>
public double TotalSize
{
get {return totalsize;}
} /// <summary>
/// 邮件内容
/// </summary>
public string Body
{
get {return body;}
} /// <summary>
/// 状态信息
/// </summary>
public string Status
{
get {return status;}
} #endregion public Pop3()
{
body = status = null;
numofmails = 0;
} /// <summary>
/// 接收信息
/// </summary>
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();
}
}
} /// <summary>
/// 通过一个独立的线程接收邮件
/// </summary>
public void ReceiveMessageAsync()
{
new Thread(new ThreadStart(ReceiveMessage)).Start();
}
}
}